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:
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.
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.
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.
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.
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.