I have a function spec defined like this, and I want to evaluate it into a function object so I can pass around.
(def spec '(foo [n] (* 2 n)))
I can create a macro like this
(defmacro evspec [name arg & body] `(defn ~name [~arg] ~@body))
then the following call will give me function foo. when called with 3, (foo 3), will return 6.
(evspec foo n (* 2 n))
However, if I get the function body from my spec defined above, the function foo returned wont evaluate the body form (* 2 n), instead, it return the body form.
(let [foo (first spec) arg (first (second spec)) body (last spec)]
(evspec foo arg body))
user=> (foo 3)
(* 2 n)
I notice the foo function created now is $eval$foo
user=> foo
#<user$eval766$foo__767 user$eval766$foo__767@39263b07>
while the working foo function is
user=> foo
#<user$foo user$foo@66cf7fda>
can anybody explain why is the difference and how can I make it work ? I want to have a way without replying on eval ? coming from javascript background, somehow I always think eval is evil.