An Overview of Clojure

Clojure is one of the most interesting languages that has emerged recently. It is a Lisp whose main goal is to be practical language for day-to-day programming. And although young, it already has a dedicated community of practitioners. Clojure has a number of features that makes it easy to learn, easy to be productive, and it already has a proven-track record in real-life applications. It also has a community that bridges the gap between industry and academia. I recently was lucky enough to go to Clojure Con, a Clojure conference, and these are my findings about the language.

Currently we find ourselves at the end of the single processor era. Most computers have multiple cores, yet they are not being fully used by modern software. So far we have been able to survive, but at some point most programming will have to take advantage of the multiple processors. At the moment we have several languages that offer easier ways to handle concurrency. Among them, there is Clojure.

Clojure is the most pragmatic Lisp that I have seen. Previous to Clojure I had exposure to Scheme and Common Lisp. Scheme is an academic language focused on teaching and research. Common Lisp was a committee compromise made 30 years ago meant to be used in the industry, but that was unsatisfactory to many.

Clojure, in contrast, has been designed for developers building commercial systems. The first feature that one notices is that rather than just having only lists as the enumerable type, it also has vectors, sets, and maps. These are specialized lists with different syntaxes to make it easy to identify them in the code. They also have different properties and performance profiles, giving the developer the flexibility to picking a different types for different tasks.

Another pragmatic decision is that Clojure compiles to JVM bytecode. So instead of coming up with a new VM, the creator used one of the most widely distributed virtual machine environments in the world. He also designed it to make it easy to interact with the Java, having strong integration with Java classes and libraries. This means that any Java library that may be useful for a project can be used in Clojure. And although the JVM is the best environment for Clojure, it is not the only one. ClojureScript is Clojure that compiles to JavaScript, and there is a project that makes Clojure compile to .NET’s CLI.

Maybe the greatest promise of Clojure is that it has been designed to make concurrent and parallel programming easy to do. Clojure adopts the immutability of some functional languages. By having immutable variables it simplifies parallel programming. At the same time it allows for rebinding “variables” with new values. In the background the values are still immutable, but it appears to the programmer to behave like a common variable.

Part of Clojure’s ecosystem is Leiningen, a dependency and package manager that makes it easier to start and manage Clojure and ClojureScript projects. In fact the recommended way to install Clojure now is to install Leiningen and let it download it and install it for you.

So all sounds nice, but is it useful? Yes, it is. Clojure is the main development language for the second biggest e-commerce site in the US. An email storage company also uses Clojure for its data management. When I asked the developer in charge of the project if he had to do anything different to make his scripts more efficient, he said that he hasn’t: the first write tends to run fast enough for their needs. Another participant said how they are using Clojure to replace performance intensive processes on a Rails application. So while the front-end and the administration of the site is done in Rails, Clojure takes care of the parts that need to run faster.

One of the presenters shared how he used Clojure for making car efficiency simulations. Thanks to Clojure he was able to speed up the development of his models, which previously were written in C++. He found that it is easier to think about the problems when you must use functions, and that it has make it possible to further tinker with his models once he had the code written. Before that he would have had to communicate with a C++ programmer and request changes. Now the problem is expressed simply enough ways that he can make the changes himself.

The previous presenter was an academic. And this highlights one of the interesting traits of the Clojure community: academics and industry developers are sharing an equal space. Although ideally every language community should have a mixture of academics and software developers, in reality some languages end up with practically all academics or all private sector developers. This makes exchanging ideas hard, so we end up with a divided software development culture. Yet this isn’t the case with Clojure. Because Clojure is a Lisp, many academics are able to bring presentations of their work to an audience of industry programmers. So one presenter shared his work, written in Scheme, on micro compiling. Another team of academics showed how to translate academic algorithms and math equations into code.

One of the interesting trends in the presentations is a move towards adding elements of typing to the language. Interesting these came from private sector presenters, who found it useful to document their data schemes and create some level of data validation. Although neither technology became a full type system, it did resemble it, and it makes me speculate whether one day there will be a number of Clojure libraries that will provide different levels of typing.

But isn’t Lisp a typeless language? Yes, it is. But being a Lisp, it allows any interested developer to create a type system if that is what they wish. In fact Clojure already has a number of libraries that allows users to use different programming paradigms. One of the most unique ones is core.logic, which allows for logic programming in the same vein as Prolog, but using a Lisp syntax. Another interesting library is one that attempts to emulate Erlang’s concurrency and fault tolerance patterns.

Clojure is a young language and it still has some rough edges. Tail recursion is a bit cumbersome to express at the moment, but it is not terrible. Also, it takes some time to understand and wrestle with some JVM issues if one is not familiar with the platform. It took me some time to figure out how to properly work with the CLASSPATH and java options. Probably the biggest problem for adopting Clojure is doing the mental shift away from object oriented thinking into functional programming. But even with the minor problems, Clojure has many thinks to offer, and it should be interesting how it evolves in the future.

This post was written by

Hugo
Senior Engineer

For more information on this post, the techniques discussed here, or Zekiah’s system integration and software development services, please e-mail us at contact@zekiah.com