I've been trying to write a macro to generate predicates from maps,
for instance the map {1 :one 2 :two} would define two predicates, one?
and two? Here's the first part of the macro:
(defmacro make-predicate [keyword-pair]
`(defn ~(symbol (str (name (nth (eval keyword-pair) 1)) "?")) [~'x]
(= ~'x ~(nth (eval keyword-pair) 0))))
So far, so good (I'm using symbol capture because x is a bound
variable here anyway). For instance:
(def nums {1 :one 2 :two})
(make-predicate (first nums))
#'user/one?
Now I try to use this macro on an entire map:
(defn make-predicates-from-map [keyword-map]
(loop [kmap keyword-map]
(if (empty? kmap) nil
(do
(make-predicate (first kmap))
(recur (rest kmap))))))
But this won't even compile: it gives a
java.lang.InstantiationException.
I guess this has something to do with keyword-map not being an actual
map yet at macroexpansion time. Whether or not that's the problem,
how do you get around it? I did try making the whole thing into a
macro:
(defmacro make-predicates-from-map [keyword-map]
`(loop [kmap# ~keyword-map]
(if (empty? kmap#) nil
(do
(macroexpand-1 (make-predicate (first kmap#)))
(recur (rest kmap#))))))
But this time I just get the InstantiationException when I run it.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en