small medium large xlarge

Hello, ClojureScript!

Clojure Reaches out to the Web through JavaScript

by Aaron Bedra

Generic image illustrating the article
  One thing nobody ever faults about JavaScript is its reach. It goes everywhere. You can’t escape it. If only using JavaScript’s reach didn’t require you to program in JavaScript. If only you could write your code in—oh, let’s get crazy—in the 21st Century incarnation of LISP, for example....  

Clojure rocks, JavaScript reaches. ClojureScript is the realized inevitability of Clojure’s expansion to other platforms.

It seems these days you can’t write a web-based application without reaching for JavaScript. That being the case, it has become pretty clear that everyone would like a better JavaScript. This isn’t a new idea, though. Look at the JVM and the CLR. Both are excellent platforms built by very talented people. Both have initial target languages that these platforms were built around. Using these platforms, language developers have built higher-level languages that allow developers to leverage better, more powerful tools. JavaScript’s “platform” has only recently become a solid foundation for languages, and we have seen some interesting things appear as a result.

ClojureScript seeks to address the weak link in the client/embedded application development story by replacing JavaScript with Clojure, a robust, concise, and powerful programming language. In its implementation, ClojureScript adopts the strategy of the Google Closure library and compiler, and is able to effectively leverage both tools, gaining a large, production-quality library and whole-program optimization. ClojureScript brings the rich data structure set, functional programming, macros, reader, destructuring, polymorphism constructs, state discipline, and many other features of Clojure to every place JavaScript reaches.

ClojureScript was released on July 20th 2011, so you can still expect a few rough edges. It is, however, functioning well and certainly worth trying. Since the full power of ClojureScript is still unclear to most, its developers have provided sample applications in the source code repository so that you can pick them up and get started quickly. Let’s take a look at one of them now.


The most complete of the samples is the TwitterBuzz application. It allows you to type in a twitter hash tag, and it reveals information about that hash tag, including who is influencing others as well as who is leading the charge. It will automatically update when possible and let you monitor a hash tag in almost real time as events unfold. It’s a great way to track news or just play around. Let’s play around.

To get started, you will need to have Oracle JDK version 6 installed, as well as git. Unlike Clojure, ClojureScript currently only targets JDK version 6 because of the use of the Rhino engine, which comes packaged in the JDK in this version. Don’t worry about the other dependencies; ClojureScript comes with scripts to help you bootstrap the rest.

First, you need to clone the repository:

 git clone git://

Once you have the source downloaded, you need to grab the ClojureScript dependencies:


After the bootstrap process is complete, you will want to do a small amount of setup to make the ClojureScript compiler tools available to your command line. The full set of instructions is available on the Getting Started page of the wiki. After your setup is complete, you can compile the TwitterBuzz code and run it in a browser. From the samples/twitterbuzz directory, use the following sequence to build the application.

 cljsc src > twitterbuzz.js

After this is completed, you can open up index.html in your browser to see everything in action. Type in any twitter hashtag and away you go!

What just happened?

If you take a look in the src directory of the example application, you will see a collection of .cljs files. Upon opening them you will notice that they don’t really look any different from the Clojure code you are used to. Feel free to browse around and examine the implementation. There are a few differences between ClojureScript and Clojure, but that discussion is for another time.

After you are through looking through the source of the twitterbuzz application, turn your attention to the out directory at the root of the project. Here is where all of the compiled code ends up. You will notice a few directories. A majority of this is supporting infrastructure, including ClojureScript itself, compiled down to JavaScript. Head in to the twitterbuzz directory to see what the cljs files were turned into.

Here you will find some fairly human-readable JavaScript code. It certainly isn’t what you would write if you were to sit down and do this by hand, but you can still understand what it is trying to accomplish. This code is what was executed when you first ran the example in your browser—this, along with the rest of the code that is in the out directory. By now you may be thinking that this is an awful lot of JavaScript, and you are 100% correct. ClojureScript has an answer for that.

Advanced Compilation

ClojureScript targets Google Closure’s advanced compiler. This piece is what really makes ClojureScript stand out. Demonstration is much more powerful than explanation here, so let’s give it a try. Run the following command from the root of the twitterbuzz project.

 cljsc src '{:optimizations :advanced}' > twitterbuzz.js

Open twitterbuzz.js again. The entirety of the twitterbuzz application is now in this one file. The Closure compiler will attempt to evaluate and eliminate all unnecessary code and compact the rest. This is much more than just minification! If you want to see more information about advanced compilation mode in ClojureScript you can do so on the wiki page.

Take a look now at index-advanced.js. Notice the difference at the bottom of the file where the script tags are. We are now down to just one. If you take a look at the size of twitterbuzz.js you can see that it is a mere 120k. It compresses down to 36k. This makes it small enough that delivery to all platforms, including mobile devices, is no longer a pain. There are even ongoing plans to reduce the advanced-mode compilation output even further.

Wrapping Up

You have seen just a tiny sliver of what ClojureScript is capable of. The ClojureScript team has a lot more up their sleeves and will continue to improve the language. You can track those changes on the github project page and follow the discussions on the mailing list.

Aaron Bedra is a developer at Relevance and a member of Clojure/core, where he uses Clojure to solve hard, important problems. He is the co-author of Programming Clojure, Second Edition.

Send the author your feedback or discuss the article in the magazine forum.