Learning Clojure

Clojure is a new functional language for the JVM. This post is a collection of links, articles, screencasts and free books I’ve found to help me learn the language and understand functional program design.

clojure icon

Key features

Here are some key features of Clojure that I find extremely interesting:

  • Designed for concurrency
  • A Lisp dialect – functional, not mixed-paradigm like Scala
  • Compiles to Java bytecode
  • Easy interoperability with Java libraries
  • Immutable data structures
  • Manage state with transactions

A lot of this seems to blow away the languages I use on a regular basis, like Ruby and PHP. Furthermore I’ve had some misgivings lately about the gaggle of run-times I end up using as a multi-language programmer using mostly interpreted languages. So the fact that Clojure compiles to Java bytecode is a real selling point for me, despite my historical avoidance of Java.

An editor

You’ll need an editor to start working with Clojure. Though I’m a Vim user, I could not get VimClojure working right on my machine. Drop me a line if you got it running – hurray for you!

If you are on Windows, I recommend the super-easy-to-install Clojure Box. After a couple of clicks, you’ll have Emacs and a working Clojure REPL. “REPL” stands for Read/Eval/Print/Loop. It’s like IRB for Ruby; an interactive development tool. Emacs makes it easy to pass code into the REPL from an editing buffer, which of course you could do with a bit more elbow grease from Vim using a REPL you spawned on the command line.

Some theory

Now you need to beef up on why Clojure is awesome. The best place is the horse’s mouth: Rich Hickey, the creator of Clojure, has released a number of essays, screencasts and lecture notes describing the benefits of his language.

Screencasts

Essays

Syntax

Time to start writing some code! A great place for an overview is the Clojure home page. However, the site can be a little overwhelming or, if you read it a bunch of times, underwhelming. So it is time to branch out and see what the world has to offer.

What we are looking for are introductory examples of Clojure code and more advanced samples of real code.

Blog posts

Not-Free Books

Sometimes, you just need to read a book. Right now, there isn’t much published about Clojure – but I recommend Stuart Halloway’s Programming Clojure.

Otherwise, a bunch of books are in “alpha” or pre-release stages from the major book publishers, so the dearth of material should improve by the end of 2010 and early 2011, I assume.

Functional programming

The big elephant in the room! The design of Clojure programs is — or ought to be — different than those written in object-oriented languages like Ruby and Java. I am still wrestling with functional program design, but thanks to the Clojure community and a lot of Googling I have found some sources to help.

Free books

Since Clojure is a Lisp, reading books on Lisp seems like the best approach to learning about the kind of designs that fit naturally into Clojure. Of course, there is also the awesome factor that your programs can interact with Java libraries, which changes things. But if, like me, you have never programmed in a Lisp or any functional language, then — first things first, as the saying goes.

Blog posts

Community

Really, the best thing would probably be to join the Clojure Google group, and to search the archives for questions you might have. A lot of OO language people seem to wash up on the Clojure beach and they (we) tend to have similar questions.

This Time It’s War

My favorite board game ever has to be Aliens, published in 1989 by Leading Edge Games.

aliens

If you’ve never played it, there is a Flash adaptation available, which seems to be free.

I was introduced to the game in the early 90s by the same guy who taught me how to play Dungeons & Dragons – an afterschool counselor named Juan. Thus I entered the hallowed halls of Geekery. On the days Juan worked, a group of Aliens-addicted kids would beg him to break out the game and let us try the suicide mission of the Reactor Room or the even-deadlier Operations & Air Ducts.

I don’t remember if we ever “won” the game (a certain number of characters have to survive for that to happen), but the taste of imminent-if-unrealized-victory and the crazy teamwork the game inspired left a lasting impression on me.

The game was out of print even when I first played. For years, as a teenager, I looked for it in comic and hobby shops around southern California, to no avail. So I gave up – and time went on, as it does. Then, several years ago, I started trolling Ebay, and I eventually snagged a good condition set for $70. Apparently it had become a collector’s item.

Since then, I have played it three times (and won twice). The last was tonight, with my wife, who for some odd reason felt compelled to indulge me.

Unpacking the box, I felt like that same excited middle-school kid explaining all of the rules to her: the aliens’ acid spray, the characters’ movement, the layout of the board. I remembered Juan explaining the same rules to me, so many years ago.

It’s a goofy game, sure – a Sci-Fi combat strategy. But it’s fast-paced and fun, and I consider it part of my childhood. After all, I spent so many days and nights thinking through possible ways to beat the game – more time than I spent on my homework! And you can’t beat the team play in a game that forces you to carry around incapacitated team members to win.

So, about tonight … Maybe having a new recruit set me back, but when we finally called it quits, we were down four characters. All of them were mine! Part of the reason, I think, was that after a bad run-in with some aliens in the southeastern quadrant of the board, I retreated my team and we changed from a split formation tactic into one group. Always a bad move.

If you play the game, I suggest splitting into two columns, and remember to always bring either Drake or Vasquez.