On Jan 23, 2009, at 16:20, Zak Wilson wrote:

> that appear in these lists and evaluate them as code. My current
> technique looks like this:
>
> (def a)
> (def b)
> (defn try-rule [r val-a val-b]
>    (binding [a val-a
>                   b val-b]
>       (eval r)))
>
> and an example call looks like:
> (try-rule '(+ a b) 2 4)
>
> I know that calls to eval are discouraged outside of writing something
> like a REPL.

Depending on how flexible your code generator has to be, this may be  
the only way to do it, which only shows that eval has its good uses,  
even if they are few in number.

One approach without eval, but also a less flexible one, would use  
rules made up of functions instead of arbitrary expression forms. You  
would then have something like

(try-rule + 2 4)

meaning that the first argument, the function +, is applied to the  
remaining ones:

(defn try-rule [f & args]  (apply f args))

You can have a more complex, but still rigid, definition of rules, e.g.

(try-rule f g 2 6 4 h 1 9)

meaning "apply f to the results of calling g and h, where g is a  
function of two arguments (as indicated by the 2 following g) and h a  
function of one argument".

If you can come up with some scheme general enough for all your  
rules, this approach is likely to be safer and more efficient than  
the one using eval.

Konrad.




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

Reply via email to