Dynamic code with clojure

I am trying to create dynamic code inside clojure. In the function below, the idea is that conditions for the macro (s) 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 will generate (and) something like this when passing keys [:tag :attrs]:

(and (= (% :tag) (rec :tag))
     (= (% :attrs) (rec :attrs)))

I started talking to various operators `` and~ `to try and get it to work, and now I'm confused. Any recommendations are appreciated.

Thank,

Colin

+3
source share
2 answers

You do not 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.

, .

+5

eval , :

(eval '(= 2 3))

, . , :

(let [a 1 b 2]
  (eval '(+ a b)))

.

- :

(def a nil)
(def b nil)

(binding [a 1 b 2]
  (eval '(+ a b)))
+3

Source: https://habr.com/ru/post/1790733/


All Articles