On Monday, March 31, 2014 12:13:25 PM UTC-5, Jony Hudson wrote:
>
>
> (defmacro functionalise
> [ex var]
> (let [arg (gensym)
> body (postwalk-replace {var arg} ex)]
> `(fn [~arg] ~body)))
>
>
This works as written, it is just that macros do not evaluate their
arguments, so you do not want to quote you expression.
(def foo (functionalise (+ 2 x) x))
(foo 40) ;=> 42
If you are working with quoted expressions, you'll have to eval in the
macro and take the one-time hit there
(defmacro functionalise2
[ex var]
(let [arg (gensym)
body (clojure.walk/postwalk-replace {var arg} ex)]
`(fn [~arg] ~(eval body))))
(def foo2 (functionalise2 '(+ 2 x) x))
(foo2 40) ;=> 42
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.