Email iconarrow-down-circleGroup 8arrow-rightGroup 4arrow-rightGroup 4Combined ShapeUntitled 2Untitled 2ozFill 166crosscupcake-iconPage 1HamburgerPage 1Page 1Oval 1Page 1Email iconphone iconpushpinblog icon copy 2 + Bitmap Copy 2Fill 1medal copy 3Group 7twitter iconPage 1

I recently ran a brief workshop on the Clojure language. Clojure is a dynamically typed functional language, and a dialect of Lisp. Functional languages place an emphasis on immutability, pure functions, and function composition. This has many benefits, like making your code more flexible, easier to test, and much easier to reason about.

Clojure’s much better than quite different from the more object oriented PHP and Javascript we usually use at UVD. I’ve been writing hobby projects in Clojure/ClojureScript for 2 years now, and I thought it was about time I spread the good word.

Introduction

Before we started doing any coding, I gave a brief introduction to the basic concepts of Clojure (slides here). In other languages, you’d need to learn about classes, infix operators vs functions, value vs reference types, why {} === {} is false… the list goes on. However, Clojure is fundamentally simpler, so you don’t have to learn everything right away. In fact, there are very few concepts you need to learn to get started. I introduced Clojure by covering the basic syntax (i.e. (function arg1 arg2)) and a bit about the different data structures. This only took about 15-20 minutes, and was enough for people to get started.

Coding

For the bulk of the workshop, we worked through the online 4Clojure problems. I wanted something that would suit people of different ability levels, and also give people something to show after the workshop was over. 4Clojure seemed like a perfect fit – the problems range from very basic to very difficult, and it gives you a nice sense of achievement. I also considered doing the Clojure Koans, but they require you to set up Java, Leiningen, and an editor, and I felt this could have been an unnecessary impediment to getting started.

I recommended people use the online RePlumb REPL, which proved to be a massive help. Using a REPL really helps when getting to grips with Clojure, as it allows you to try out the core functions. RePlumb even gives you a helpful reference of the most common core functions. Even after you’re more comfortable with Clojure, using a REPL dramatically speeds up development by allowing you to develop interactively, trying out functions as you write them. As Daniel Higginbotham says in “Clojure for the Brave and True”:

REPL development is an essential part of the Lisp experience, and you’d really be missing out if you didn’t use it.

Everyone made their way through the basic exercises, and some of us made it onto the harder problems. It seemed like everyone enjoyed themselves, and managed to get to grips with the basics of the language. We didn’t quite get onto writing our own transducers though unfortunately .

Conclusion

Although the workshop went pretty well overall, I did find there were a few non-intuitive bits of Clojure that people struggled with:

  • The difference between lists and vectors. Explaining to someone new to programming why cons works differently for lists and vectors is difficult.
  • Error messages. They has been voted the top pain point for 3 years running in the “State of Clojure” surveys, and they have been debated for years in the Clojure community. On a few occasions, people got very cryptic error messages, like a ClassNotFoundException when they were missing a closing parenthesis. Hopefully they’ll be improved by clojure.spec in the near future, but for the time being it’s very off-putting to beginners.
  • Lots of Parentheses. The amount of parens is one of the common complaints about all lisps. I think the problem is entirely mitigated by using Paredit and the threading macros, but for absolute beginners it can be a turn off.

Even with these difficulties, I was surprised at how fast people got up to speed with Clojure, especially considering that the functional paradigm is so different to OOP. If I’ve sparked your interest in Clojure, I’d recommend reading “Clojure for the Brave and True”, and working your way through the 4Clojure problems.

 

Share: