Sunday, September 9, 2012

A quick review of Clojure

I just want to start off by saying that I do NOT KNOW CLOJURE. I have only experimented a little bit with it and do not have any professional opinions about it. These are my thoughts as a newcomer to the language coming from a Java / lisp background.

A bit about Clojure
Clojure is a Lisp dialect that compiles directly to bytecode for the Java Virtual Machine (JVM) like Java itself does. This gives it a property called Java Interopibility. This is one of Clojure's selling points. Java and Clojure are both completely compatible with one another in a compiled format. This means that you can use Java libraries in your Clojure project and vice versa. Even Swing / Awt graphical user interfaces can be integrated with your clojure project just as they would be in Java.

The Good
To start off Clojure IS a Lisp, and Lisp's syntax is terse and beautiful </opinion>. Clojure has fixed some of Lisp's problems, especially the awful way for handling iteration. In Clojure there is a macro called loop just like in Lisp, but the syntax is easier to follow. Here is an example using loop.
(loop [x 5] (print (concat "looping " (str x) " more times")) (if (= 0 x) nil (recur (- x 1))))
This is obviously written in terrible form since it is pretty procedural, but this is supposed to showcase how loop works. It is a let form that can be returned to with recur. The magic here is that this does not create a stack overhead. No more stack overflows! Of course, infinite recursion can become a problem. But now recursing can be much lighter on the resources and you no longer need to declare a local function to recurse.

The best thing about Clojure is the Java interop, though. Java is one of the most widely used programming languages out there, running on billions of devices and being responsible for Android, desktop applications, and embedded systems in some cases. When you use Clojure you do not lose this massive support, or any Java libraries you have coded up in the past.

Clojure is an open-source project and you can view the source code for all of the base Clojure functions right on the github page.

The not-so-good
Clojure has a confusing means of editing data directly since just about everything but variables (declared with (def)) are immutable; they cannot be changed. This pretty much forces the functional paradigm down the programmer's throat and can actually halt good practice in some cases.

The language is young and not very well known. Its popularity is on the incline into the mainstream, but for now it is still an obscure language. Very few practical programs are written in Clojure and the documentation leaves a bit to be desired on the official Clojure website.

Also due to its lack of popularity as of right now there are few Clojure libraries to play with. However, Java Interop makes this less of a problem than it would with other languages (like Common Lisp, for instance).

The tools used to write Clojure are not as good as Common Lisp's yet, in my opinion at least. Emacs does not have a Clojure-mode, but one can be added through a third-party repository (although I could not get it working for the life of me on Emacs 24). Eclipse also has a plugin called counterclockwise that hooks up to the Lein REPL and provides tools for saving and compiling Clojure.

Clojure Resources
#clojure on Freenode IRC seems like _THE_ meeting place to discuss clojure and ask for help, or just to brag about something you wrote in Clojure. Even Clojure Programming by Emerick Carper & Grand , the de-facto Clojure book for beginners and intermediate Clojure programmers mentions #clojure.

No comments:

Post a Comment