I’m trying to create some dynamic code within clojure. In the function below, the idea is that the conditions for the (and) macro will be dynamically generated.
(defn matching-keys [rec match-feed keys]
(> (count (clojure.set/select #(and (for [k keys]
(= (% k) (rec k))))
(set match-feed)))
0))
So if it worked!! then this code would produce an (and) something like this when passed keys of [:tag :attrs]:
(and (= (% :tag) (rec :tag))
(= (% :attrs) (rec :attrs)))
I’ve been messing around with various `` and~` operators to try to make it work, and am now in a state of confusion. Any guidance is welcome.
Thanks,
Colin
You don’t need dynamically generated code for this. Changing the anonymous function to
#(every? (fn [k] (= (% k) (rec k))) keys)should do what you want without generating code at runtime.The ability to use higher-order functions means that you should hardly ever need to dynamically generate code.