ClojureScript saved me 100 hours
Tags: clojure, clojurescript, games, programming
Published: 2015-07-28 00:00:00

---
title: ClojureScript saved me 100 hours
published: true
description: Three quiet evenings with ClojureScript to save 100 hours of JavaScript
cover_image: 
tags: clojure, clojurescript, games, programming
---

[Five years ago][1], 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][2] and [Simply Scheme][3] textbooks. Infact, I think I may still be helping some first-year Comp Sci students at Berkeley University through my [answers repository][4] 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][5]. 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][6] 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:

```clojure
(. Thread sleep 10)
```

was simply replaced with:

```clojure
(.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][7]. 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!

[PLAY MOON DWELLER NOW][8]

Happy hacking, people.


  [1]: https://github.com/buntine/Medieval-Alien-Massacre/commit/101082f3d99317a10a27da9e544bcc650b91b565
  [2]: https://mitpress.mit.edu/sicp/
  [3]: https://www.cs.berkeley.edu/~bh/ss-toc2.html
  [4]: https://github.com/buntine/Simply-Scheme-Exercises
  [5]: https://github.com/buntine/Medieval-Alien-Massacre
  [6]: https://github.com/clojure/clojurescript
  [7]: https://github.com/buntine/MoonDweller
  [8]: http://moondweller.io/