On 31/03/14 23:51, François Rey wrote:
On 31/03/14 23:25, Lee Spector wrote:
(defn functionalise
   [ex var]
   (eval (list 'fn (vector var) ex)))

(def ex '(+ 1 x))

(def exf (functionalise ex 'x))

(exf 3) ;; => 4

This calls eval only once when you call functionalise, and doesn't do any tree 
walking.

FWIW this is the trick I use in the tiny genetic programming system 
at:https://github.com/lspector/gp/blob/master/src/gp/evolvefn.clj

  -Lee

That would not work with a symbol other than x:
(defexf  (functionalise  ex  '*z*))
How about this:

(defnfunctionalise  [ex  var]
   (eval`(fn[~(clojure.core/symbol  "xp")]
            ~(clojure.walk/postwalk-replace  {var'xp}ex))))

Forget that, my code does not work with 'z too, obviously.
Too late, time to go to bed for me....

--
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