Thursday, July 2, 2009

Opening your mind with Clojure

I know, I know, clever title, right? It's true though--I've taken the last week or so to take a stab at learning Clojure. I've played with functional languages before, but unfortunately "play" is about as far as I've gotten. This time it was different. I actually made it all the way through Programming Clojure (Pragmatic Programmers), which is saying something (I'm king of "Oh, I'll buy THAT book too!" and never finishing it...). There are a variety of reasons I'm really enjoying Clojure, but for now I'd like to offer some tips in getting set up.

Getting the Source



Rich Hickey is the guy behind Clojure. I'd call him a mad genius, except he seems pretty nice and "happy genius" doesn't really work...Anyway, he's recently agreed to move the project to github, and you can find the two main sources you'll want in his account there. Using the wonderfully helpful github gem you can clone things down with


gh clone richhickey/clojure
gh clone richhickey/clojure-contrib


At this point I'm assuming you've got java installed and working, which may be a big assumption, but I don't have the heart to tackle that mountain today.

So, get into the source and start compiling:


cd clojure
ant


If all goes well, you should have some nice jars all sparkling and ready for use. Now for contrib:


cd ../clojure-contrib
git checkout 3073f0dc0614cb8c95f2debd0b7e6a75c1736ece ***
ant -Dclojure.jar=../clojure/clojure.jar


*** At this point in time, you need this revision to compile against clojure 1.0. I imagine this will change over time, but if you have any problems with compilation, a good place to check is the #clojure channel.

You need to pass in that -D flag so we can use the clojure.jar in compiling clojure-contrib. Once this is done you'll want to put the jars into a common place where java can load them up. I've created an /opt/jars directory on my system, and so I'll drop them into here.

Now, you'll want to make sure the CLASSPATH variable is setup correctly, and it took me a while to do this the first time, so here's a hint:



I think newer versions of java will just let you specify the main directory name. This is for java 1.5 which I somehow seem to still be running.

Almost there!

Setting up a clj file



I've blatantly stolen this file from Chouser, an ever present and extremely helpful soul found in #clojure (he's like Illari of #git for those who frequent that channel...if I ever get rich, a fat check is in their future).

This is what the clj file looks like:



If you're on OS X you'll want to install rlwrap:


sudo port install rlwrap


You'll also want an init file to help load up some useful functions like 'show' into your REPL. Create a file named "repl-init.clj" and put something like this inside:


(set! *print-length* 103)
(set! *print-level* 15)
(use '[clojure.contrib.repl-utils :only [source show]])
(use '[clojure.contrib.stacktrace :only [e]])


This should be executed by your clj script on startup, and setup things automatically for you.

Wrap-up



So that's about it for now. I'm having a great time so far, and honestly the hardest parts are getting your environment setup and keeping it up to date. If you're used to java development then this should be a breeze. Having been away from it for years, it took me a while, and hopefully this will save others that same pain.

Thanks to Paul Barry and Aaron Bedra for getting me interested in Clojure at Ruby Nation, and of course Rich et al for all the time and energy put into developing this great new language.

A few links



clojure.blip.tv - Rich gives some great presentations, and many of them are captured here.
clojure.org - Lots of great documentation here
#clojure on irc.freenode.net - over 100 clojurists and clojuristas. Very helpful and friendly.

5 comments:

Paul Barry said...

Another trick you can do to get the classpath setup is to use the
-Djava.ext.dirs argument to java and give it a directory. Then any jars you put in that directory will be on the classpath

Jack Dempsey said...

Oh excellent Paul, that actually explains part of whats going on in the clj script I linked to. Guess I can remove that ugliness from my bash_login now. Thanks!

Joe Grossberg said...

You should try "The Little Schemer". It's the only FP book I've made it through -- and uses a really interesting Q&A-style pedagogy.

Jack Dempsey said...

Thanks Joe, I've heard of it, but never had a chance to read it.

I imagine it will be a good backup plan to further learn some pure FP.

Anonymous said...

Jack, thanks for writing up all the necessary little tips from the legwork you had to do!