Back to Blog

Trying out ClojureScript

The Christmas holidays are a perfect time to try and learn new things. A few weeks ago, I helped organize a Code Club session with my colleague Tiina Romu at the English School. She had created an exercise to draw snowflakes in the Python in Browser environment. I thought it would be fun to experiment my own implementation as well.

Derek Slager’s recent, nice presentation ClojureScript for sceptics had made me thinking that the time is ripe to start learning ClojureScript as well. Here’s what I learned:

  1. It might be a good idea to first read the Quick Start tutorial but the journey might not be that quick in the end. It is good to understand what’s really happening when you build your ClojureScript source to JavaScript, but it’s much easier to build your project with e.g. lein-cljsbuild or Figwheel. It might also help to start with a suitable Leiningen template.
  2. After you have set up your build process, you’ll want to build changes automatically to skip the JVM startup time hit. If you’re using lein-cljsbuild, use: $ lein cljsbuild auto
  3. The ClojureScript platform is developing fast, so make sure you are using the newest version of both Clojure and ClojureScript languages and tools.

  4. Check the Differences from Clojure. Not all Clojure features are available in ClojureScript. Some examples
    • One cannot e.g. require all functions from another namespace with (ns some.ns (:require [some.dep :refer :all]))
    • E.g. Ratio, BigDecimal, and BigInteger literals are currently not supported.
    • Naturally, one cannot use Java dependencies, so e.g. instead of java.lang.Math you have to use the JavaScript Math lib.
  5. The JavaScript interoperability is uglyish at times: (def element (aget (js/document.getElementsByTagName "main") 0))
  6. You have all the Clojars libraries in your fingertips with an easy build tool
  7. Once you get the compiled code running in the browser, the source mapping works nicely and you can check the errors right from the browser’s development console!

In the end, I got my Christmas greeting done. You can find it at http://ykarikos.github.io/cljs-flake/. The source code is shared at https://github.com/ykarikos/cljs-flake.

![Merry CLJS Christmas](http://ykarikos.github.io/cljs-flake/)

I really like functional programming in LISP syntax and thus it has been a pleasure to learn Clojure. However, at the same time I haven’t had the time to learn the tooling and frameworks needed for modern Javascipt development. However, it seems that ClojureScript will be – at least for me – an easier way to start building modern web frontends. The platform has advanced a lot this year. David Nolen has listed the highlights in ClojureScript Year In Review.

So, what might I try next?

Author

  • Yrjö Kari-Koskinen
    Principal Consultant