On Feb 4, 8:11 pm, Bryce <fiat.mo...@gmail.com> wrote:
> I take your point; I've given up trying to actually define a function
> with the expression for the moment (I'd imagine it's still possible,
> just much trickier than I thought).  My intention was to fake operator
> overloading.  For my purposes it should be enough to evaluate the
> expression with substituted functions, which is very easy:
>
> (defmacro subFuncs [arg1 arg2]
>   (eval `(replace ~arg1 ~arg2))
>   )

I'm not entirely sure I understand what your exact requirements are,
but you can resolve most problems without resorting to eval. The
easiest way to locally use a different function in place of a pre-
existing one is to re-bind its symbol:

user=> (+ 1 2)
3
user=> (let [+ (fn [x y] (+ x y 1))] (+ 1 2))
4

If you need the "replacement" to work across function calls (i.e.
dynamically scoped), you could use binding to re-bind the Var that
stores the function:

user=> (defn add [x y] (+ x y))
#'user/add
(user=> (map add [1 2] [3 4])
(4 6)
user=> (binding [add (fn [x y] (- x y))] (map add [1 2] [3 4]))
(-2 -2)

Note that I didn't use the + function directly here since the compiler
would have inlined it, rendering the re-binding ineffective. The
binding form is also restricted to the current thread, so beware when
using things like pmap and future.

Now, if your replacement requirements are really exotic and you
absolutely want to write a macro, take a look at clojure.walk/prewalk-
replace and postwalk-replace.

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

Reply via email to