On 12.05.2009, at 05:42, Mark Reid wrote:

> I'm quite new to macros so forgive me if this is a naïve question, but
> is it possible to write macros that are applied to an entire Clojure
> program?

It depends on what you call a "program". Clojure code is structured  
in the form of namespaces, and inside each namespace there is a  
sequence of top-level expressions that are evaluated in order.  
Launching a program written in Clojure means launching a function in  
a namespace, which would typicall call other functions from several  
other namespaces. Some of these would typically be part your  
"application", other would be in libraries or in the Clojure core.

I don't think there is any way to apply a macro to all of that  
without modifying Clojure itself. I also don't think there is a good  
reason to apply a macro to everything. If you find a way to speed up  
basic operations in Clojure that are of general interest, you'd  
better propose a patch to Clojure than write your own compiler on top  
of it.

What can be done fairly easily is apply a macro to all of a  
namespace. If you macro is called "optimize", you insert the line

(optimize

right below the ns form and a line

)

at the end of the namespace. But I am still not convinced that this  
is a good idea, as you would still be writing a compiler on top of  
Clojure. As with all optimization, I would recommend to first  
identify the parts of your program that are the performance  
bottleneck, and then optimize those.

> The reason I ask is that, in other threads in this group, some simple
> transformations to improve efficiency of Clojure programs were
> mentioned. In particular, it seems converting `(+ 1 2 3)` to `(+ 1 (+
> 2 3))` can speed things up.

How much do you expect to gain from this? I didn't see a discussion  
on this topic before (but I don't read all messages on this group).

> Applying such a transformation to my own code would be a mindless  
> pattern matching job, and thus perfect for automation.

Indeed, and macros are the right tool for that. But if you want to  
make such a macro general enough that it is safe to apply to  
arbitrary code, expect to spend a lot of time on writing that macro.  
It is easy enough to write a macro that goes through a form,  
identifies + subforms, and rewrites them. However, not every form  
beginning with + is a call to clojure.core/+. It might be a call to a  
function called + in another namespace (for example  
clojure.contrib.generic.arithmetic/+), or it might be part of a macro  
template or some other quoted expression.

For these reasons, I'd suggest to apply your optimizations only to  
code that you know well enough to be sure it doesn't contain some  
tricks you might not have thought about.

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