Thursday, 31 January 2013

A Language for All Seasons

I've been slowly learning Clojure for a while now, so I decided to try it out on a real project.

A year or so ago I wrote a system to compile a static website. It's completely tailored to the website I use it for. Its a mix of XML, XSLT and Java. It seemed like a good candidate as it is a non-trivial use-case, but also because if it doesn't work I can fall back on the Java solution, and if it does I can write a nice 'compare and contrast' for my blog. ;-)

Early signs look good.

The declarative nature of Clojure, and the 'Code Is Data' approach of LISP, made the data part of the system trivial. Where once was XML, now all is Clojure. And its a surprisingly good fit. Its already less verbose than XML, and I've hardly polished it at all (I haven't written any macros yet).

In addition, by declaring the data directly with function calls, the transformation previously handled by XSLT is also trivial. Just change the function implementations and the output changes. Its a bit like XSLT (being a functional language), but the syntax is... ...well, it isn't XML. Which is nice.

And when I want to do fiddly little bits of file manipulation (copying resources around, for example) and plumb the whole thing together to turn input into output I can do everything I did in Java in Clojure.

But Clojure hasn't just turned out to be a better XML, XSLT and Java than XML, XSLT and Java respectively. I get the additional benefit of no impedence mismatch at the boundary between them. Is this bit of transformation appropriate for XSLT? Is it better suited to Java? Or should I restructure the data in XML? I still need to make decisions, but its so much easier to change my mind and move bits around, because the whole system is in one language.

I'm still a little way off finishing the job. But I will definitely see it through. If nothing else, I'm having too much fun!