Here's a reason why a partial application macro is better than a
partial application function:

user=> ((partial or (prn "Hello")) (prn "World"))
java.lang.Exception: Can't take value of a macro: #'clojure.core/or
(NO_SOURCE_FILE:2)

user=> (($ or (prn "Hello")) (prn "World"))
"World"
"Hello"
nil
user=> (or (prn "Hello") (prn "World"))
"Hello"
"World"
nil

This shows what I mean about evaluation: the parameter (prn "World")
gets evaluated before (prn "Hello"), so the messages get sent in a
different order than if you had not used partial application. I tried
to figure out a way for a macro to return a macro, to try to further
delay the evaluation of the non-partially-applied parameters, but
couldn't figure out a way to do it.

Thanks for showing me the auto-gensym facility. I couldn't look up the
documentation when I wrote this because my internet was out. All I had
to consort me was core.clj.

On May 28, 1:22 am, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> Here is a somewhat simpler definition :-) :
>
> user=> (def $ clojure.core/partial)
> #'user/$
> user=> (def add5 ($ + 5))
> #'user/add5
> user=> (add5 3)
> 8
> user=>
>
> But maybe you want the delayed evaluation of the already included
> arguments, but it is not clear to me from your e-mail (and your
> example that uses a literal integer), and it's also not clear to me
> what uses cases may require this macro. Could you elaborate a little
> more on the subject ?
>
> Please also note the gensym facility of clojure (get rid  of the
> gensym, and autocreate a gensym by suffixing a # to a symbol inside a
> syntax quote expression) :
>
> (defmacro $ [f & args]
>    `(fn [& args2#]
>       (eval (cons (quote ~f) (concat (quote ~args) args2#)))))
>
> See:
> user=> (macroexpand `(args2# args2#))
> (args2__44__auto__ args2__44__auto__)
> user=>
>
> Regards,
>
> --
> Laurent
>
> 2009/5/28 kinghajj <kingh...@gmail.com>:
>
>
>
> > (defmacro $ [f & args]
> >  (let [args2 (gensym)]
> >    `(fn [& ~args2]
> >       (eval (cons (quote ~f) (concat (quote ~args) ~args2))))))
>
> > Example:
> > (def add5 ($ + 5))
>
> > (add5 3)
>
> > Beware! For this macro evaluates the later parameters before the
> > partially-applied ones, so side-effectful parameters may occur in an
> > unexpected order.
>
>
--~--~---------~--~----~------------~-------~--~----~
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