Hi all,

 I've gotten myself stuck with what is probably a simple question, and 
wonder if someone can advise. What I want to be able to do is to take a 
symbolic expression, like '(+ 1 x) say, and turn it in to a function 
programmatically, so that I can call something like:

(def ex '(+ 1 x))
(def exf (functionalise ex 'x))
(exf 3) ;; => 4

where functionalise is the thing I want to implement, and it's taking the 
symbol to treat as an argument/variable in its second place. I can come up 
with a nasty solution:

(defn functionalise [ex var] (fn [xp] (eval (postwalk-replace {var xp} 
ex))))

but this has the significant downside that it does the walking every time 
the resulting function is called! 

Not being much of a macro-writer, I tried the following:

(defmacro functionalise
  [ex var]
  (let [arg (gensym)
        body (postwalk-replace {var arg} ex)]
    `(fn [~arg] ~body)))

But it doesn't work, in the sense that evaluating it gives something like (+ 
1 G__6779) .

Like I say I've not got much macro experience, and feel like something 
hasn't clicked in my head yet. Any clues?

Thanks in advance,


Jony

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to