Choose functions over macros when you can write an equivalent function.

I use macros when:

a) I have a repetitive pattern in the code that cannot be easily turned
   into a function (too much context to pass as args to a function).
   You can then hide a binding form in the macro to refer to the context
   or directly refer to it (global vars, ...) using form expansion.
   You can wrap huge chunks of your code in the macro referring to the context
   ( & body) easily.

b) I need to evaluate the args (aka the symbols) to alter the form(s)
   generated by the macro and do not want immediate evaluation.

c) I need a lighter syntax. You can marshall the symbols themselves as you 
   wish to refer to other context bindings by sufixing/prefixing the synbol
   names.

   I wrote an interactive report utility used by non-lispers and that helped
   remove some Clojure syntax requirements that would look obscur
   to non-Lispers. The report utility can refer to global bindings while
   the user uses nicknames and other syntatic sugars to simplify the calls.
   This hides references to the context that would clutter the report code
   and make the tool unusable for the average user.

d) I see some potential in tuning the forms later (maybe to create functions)
   but do not want to embark on that journey now. Using macros you can defer
   retooling of your code or hide partial retooling until you are ready
   to change the top form to its definitive look.


Luc P.

jbk <julian.b.kel...@gmail.com> wrote ..
> I'm new to Clojure and just getting my head around macros. As an
> exercise I was trying different ways to wrap making a proxy for
> java.util.Comparator and came up with two alternatives. I'm really not
> sure how to judge what would favour one solution over the other, and
> I'm curious if one style is preferred over the other around the
> Clojure community.
> 
> First some data:
> 
>     (def things #{:q :w :e :r :t :y :a :s :d})
>     (defn inverse-compare [a b] (* -1 (compare a b)))
> 
> Using a macro to generate a proxy to change sort order:
> 
>     (defmacro cmprtr [f] `(proxy [java.util.Comparator] [] (compare
> [a# b#] (~f a# b#))))
>     (sort (cmprtr inverse-compare) things)
> 
> Using a simple function to generate the proxy:
> 
>     (defn cmpprx [f] (proxy [java.util.Comparator] [] (compare [a b]
> (f a b))))
>     (sort (cmpprx inverse-compare) things)
> 
> 
> Thanks,
> Julian.
> 
> -- 
> 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 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