ClojureScript saved me 100 hours

buntine profile image Andrew Buntine ・3 min read

Five years ago, I started tinkering with Clojure, a Lisp that runs atop the JVM. The language was still somewhat esoteric at the time. It was a Lisp, afterall!

Picking up Clojure wasn't too difficult because I'd already spent a few years playing around with Scheme. Generating fractals, Fibonacci sequences (of course) and working my way through the SICP and Simply Scheme textbooks. Infact, I think I may still be helping some first-year Comp Sci students at Berkeley University through my answers repository to the latter. Hey Berkeley, how about an honorary degree? Oh c'mon...

It was obvious from the get-go that Clojure was going to be a big deal. Although the language designer, Rich Hickey, decided to stick with S-Expressions, Clojure came with a much richer set of data structures than the humble linked list. And they were immutable and persistent, which gave them both the thread safety of immutability and the performance characteristics of mutability. The whole Java interop thing was also very important... It meant that Clojure programmers had access to the plethora of existing Java libraries.

The first non-trivial software I wrote in Clojure was a text-based game called Medieval Alien Massacre. It was inspired by an old game called Dunnet that, funnily enough, still ships as a default package with the Emacs editor today. The game was great fun to write, although I made the mistake of choosing a title and publishing the game before I'd written the story. The finished product did not involve any massacres and did not occur in the middle ages. It did, however, contain aliens so I wasn't completely wrong!

But, more importantly, sharing the game with my non-techy friends was kind of difficult as it required them to have Java installed and run it in a shell. I decided that I would (one day) rewrite the game in Javascript so that I could utilise the browser. As far as ease-of-distribution goes, it's difficult to beat Javascript.

And so sat my game. Incomplete and unknown. Alas, such is the blight of the the computer programmer... Until, that is, a friend mentioned to me that a project called ClojureScript was gaining some popularity. ClojureScript is a Clojure to Javascript compiler, which meant, atleast theoretically, I could directly compile Medieval Alien Massacre to Javascript and save myself the hassle of doing it manually!

I knew it wouldn't "just work". I was using Java's threading library to suspend execution, doing user IO from the terminal, etc. And so I started through the code, line by line, flagging everything that I thought might not compile. The process turned out to be relatively simple. ClojureScript swaps out Java interop for Javascript interop and so, for example:

(. Thread sleep 10)

was simply replaced with:

(.setTimeout js/window f 10)

I replaced all IO with a simple HTML input wrapped in a form with a submit event attached to it. I'd say about 85% of the core codebase stayed exactly the same. The language parsing didn't change at all!

In total, the process took me three quiet evenings after work. I also took the chance to give the game a more appropriate name: Moon Dweller. At a guess, I reckon I would have spent atleast 100 hours writing my own version in Javascript.

Oh, and guess what?! You can play it right now directly in your browser!


Happy hacking, people.

Posted on Jul 28 '15 by:

buntine profile

Andrew Buntine


Conjurer of computational spells and trickery.


markdown guide
> eat candy bar
You don't have that item...
> take candy bar
> eat candy bar
You feel like you just ate crusty skin off Donald Trump's forehead. Although inside the wrapper there was an 'instant win' of 5 credits!

Thanks for sharing and congrats! :-)