I have a function specification defined like this and I want to evaluate it in a function object so that I can pass.
(def spec '(foo [n] (* 2 n)))
I can create such a macro
(defmacro evspec [name arg & body] `(defn ~name [~arg] ~@body ))
then the next call will give me the foo function. when called with 3, (foo 3) will return 6.
(evspec foo n (* 2 n))
However, if I get the function body from my spec above, the function foo returns not to evaluate the shape of the body (* 2 n), instead it returns the shape of the body.
(let [foo (first spec) arg (first (second spec)) body (last spec)] (evspec foo arg body)) user=> (foo 3) (* 2 n)
I noticed that now the created function foo is equal to $ eval $ foo
user=> foo #<user$eval766$foo__767 user$eval766$foo__767@39263b07 >
while the working function is foo
user=> foo #<user$foo user$foo@66cf7fda >
can someone explain why the difference is and how can I make it work? I want to have a way without responding to eval? based on javascript background, I always think that eval is evil.
source share