I am experimenting with clojure macros and wonder what can I do wrong?
I have a simple example: try to dynamically create functions based on a map.
For example:
(def units {:cm 100 :mm 1000 :m 1 :km 1/1000}) (defn m-to-unit-helper [[kv]] (let [f (symbol (str "to-" (name k)))] `(defn ~f [m#] (* ~vm#)))) (defmacro m-to-units [units-map] (let [funcs (map m-to-unit-helper units-map)] `(do ~@funcs ))) ; complains with: Don't know how to create ISeq from: clojure.lang.Symbol (m-to-units units) ; To try and debug (defn debug [units-map] (let [funcs (map m-to-unit-helper units-map)] (clojure.pprint/pprint `(do ~@funcs )))) ; see below (debug units)
The macro does not work, but the debug output looks like it should create the correct structure:
(do (clojure.core/defn to-mm [m__32709__auto__] (clojure.core/* 1000 m__32709__auto__)) (clojure.core/defn to-m [m__32709__auto__] (clojure.core/* 1 m__32709__auto__)) (clojure.core/defn to-cm [m__32709__auto__] (clojure.core/* 100 m__32709__auto__)) (clojure.core/defn to-km [m__32709__auto__] (clojure.core/* 1/1000 m__32709__auto__)))
Any advice would be greatly appreciated. Thanks.
macros clojure
toolkit
source share