From 0db4cf7f7ac5ce087c0d53dd5f99019e1dca289b Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Tue, 16 Jul 2013 11:20:26 -0400 Subject: [PATCH 1/2] imported kevin's three token-parser choices into core.clj --- src/hs_robotwar/core.clj | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/hs_robotwar/core.clj b/src/hs_robotwar/core.clj index 8ebc406..13a9f55 100644 --- a/src/hs_robotwar/core.clj +++ b/src/hs_robotwar/core.clj @@ -63,18 +63,36 @@ (def return-err (ignoring-args-thunk "Invalid word or symbol")) +(def parser-priority + [[registers :register] + [commands :command] + [str->int :number] + [valid-word :label] + [return-err :error]]) + (defn parse-token "takes a single token and adds the appropriate metadata" - [{token-str :token-str, pos :pos}] + [{:keys [token-str pos]}] (some (fn [[parser token-type]] (when-let [token-val (parser token-str)] {:val token-val, :type token-type, :pos pos})) - [[registers :register] - [commands :command] - [str->int :number] - [valid-word :label] - [return-err :error]])) + parser-priority)) + +(defn parse-token-2 + "takes a single token and adds the appropriate metadata" + [{:keys [token-str pos]}] + (first (for [[parser token-type] parser-priority + :let [token-val (parser token-str)] + :when token-val] + {:val token-val, :type token-type, :pos pos}))) + +(defn parse-token-3 + "takes a single token and adds the appropriate metadata" + [{:keys [token-str pos]}] + (some identity (for [[parser token-type] parser-priority] + (when-let [token-val (parser token-str)] + {:val token-val, :type token-type, :pos pos})))) (def value-type? #{:number :register}) From 8c2a8f7841dd568b16fe6f779bbe3e729d9dbc08 Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Tue, 16 Jul 2013 11:26:36 -0400 Subject: [PATCH 2/2] chose for comprehension with :let and :when bindings option for parse-token --- src/hs_robotwar/core.clj | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/hs_robotwar/core.clj b/src/hs_robotwar/core.clj index 13a9f55..b15a68c 100644 --- a/src/hs_robotwar/core.clj +++ b/src/hs_robotwar/core.clj @@ -71,15 +71,6 @@ [return-err :error]]) (defn parse-token - "takes a single token and adds the appropriate metadata" - [{:keys [token-str pos]}] - (some - (fn [[parser token-type]] - (when-let [token-val (parser token-str)] - {:val token-val, :type token-type, :pos pos})) - parser-priority)) - -(defn parse-token-2 "takes a single token and adds the appropriate metadata" [{:keys [token-str pos]}] (first (for [[parser token-type] parser-priority @@ -87,13 +78,6 @@ :when token-val] {:val token-val, :type token-type, :pos pos}))) -(defn parse-token-3 - "takes a single token and adds the appropriate metadata" - [{:keys [token-str pos]}] - (some identity (for [[parser token-type] parser-priority] - (when-let [token-val (parser token-str)] - {:val token-val, :type token-type, :pos pos})))) - (def value-type? #{:number :register}) (defn parse