Why I love Clojure?

Tomasz Czermiński

Clojure language as one of the modern Lisp’s dialects was carefully designed with concurrency in mind. It enables you to communicate between many machines seamlessly. Let us take a look at what Clojure can offer us.

Introduction

Several years ago none of us has heard about the Big Data, Artificial Intelligence or Microservices. The world keeps changing. One would say that for better, the others for worse. The change is the immanent part of life. On the other hand, I do not think core things change that quickly. For example, do you know Lisp? It was invented in 1958 by John McCarthy and since then it has been widely used in AI field. It has almost no syntax and yet it enables a user to describe a problem in an elegant and concise way.

The reason I write about all of this is that people keeps telling Lisp is long dead and buried, but in fact it is not and never was. The truth is that nowadays – in the era of distributed computing – we need a power of Lisp, its expressiveness and simplicity. Clojure language, as one of the modern Lisp’s dialects, is a great evidence that the language is still alive. It was carefully designed with concurrency in mind. It enables you to communicate between many machines seamlessly. I strongly encourage you to consider using it in your next project. Let us take a look at what Clojure can offer us.

Rich ecosystem

Clojure runs on top of the JVM [1] and as a result it has a full access to all of the  libraries available in Java. Furthermore, as Clojure code is compiled down to the bytecode, it is easy to integrate with an existing legacy codebase. IMHO, this one fact is the most important of all, so let us focus on it.

I am sure that any company that has a ten (or more) years old product built with, let us say, Java must have its own set of internal libraries. In such a case, there is a non-zero probability that some of these libraries must be imported by each of the newly created internal applications. This is not an issue in Clojure (nor it is in Scala, Kotlin, Groovy or any other JVM-based language). As long as a library you want to use is just a JAR archive, you can place it on your classpath and import it in your Clojure script.

POL – Plain Old Lisp

As I have already mentioned Lisp’s expressiveness, simplicity and elegance is something important as well. Lisp was created as a practical mathematical notation for computer programs and as such it brings beauty of mathematical abstraction into the program.

There is one thing that needs to be pointed out. The fact is that simplicity is not the same as easiness. I think that being simple is the same as being free from complexity whereas being easy is more like being familiar.

Let me show you the difference. This is a Clojure’s function for finding nth element in the Fibonacci sequence:

(defn fibonacci
  "A function calculating nth element of the Fibonacci sequence."
  [n]
  (cond
    (= n 0) 0
      (= n 1) 1
        :else (+ (fibonacci (dec n)) (fibonacci (- n 2)))))

 And here is Java’s equivalent: 

/**
 * A function calculating nth element of the Fibonacci sequence.
 */
int fibonacci(final int n) {

    int a = 0;
    int b = 1;

    for (int i = 0; i < n; ++i) {
        a = a + b;
        b = a - b;
    }
    return a;
}

The difference between these two above is that in Clojure we are focused on a problem, not on language syntax. It is easier to reason about.

The truth is that for most of us Lisp is not easy, but still it is simple. Rich Hickey – the author of Clojure – had a great talk about Simplicity in a context of a software development. [2]

Lisp stands for “LISt Processor“. Everything in a Lisp program is a list. Even the program itself (or to be precise its source code). IMHO, the implications are huge. If an application is just an ordinary list, it can be manipulated and modified just like a normal data structure. In Clojure we have macros which make creating a DSL (Domain Specific Language) remarkably easy. DDD (Domain Driver Design) is only a step further from this.

Summary

Rich Hickey looked for programming language with first-class support for functional programming, designed for concurrency and symbiotic with an established platform. And he could not have found one. Clojure is not so-called pure functional programming language. In theory, it lets you build stateful applications with side effects. However, there are two reasons that make it not harmful. Firstly, state is guarded by software transactional memory. It means that operations which mutate a state are performed in transactions (similar to the database transactions). Secondly, programs without side effects do not exist in the real world. Even Haskell provides a way to interact with outer world via IO Monads. To sum up, Clojure is a pragmatic language, for real world distributed systems with rich ecosystem provided by the JVM.

[1] To tell the truth, in the beginning Clojure was supported on .NET platform as well but I do not know if it is a truth anymore.

[2] Rich Hickey – Make Software Development Simple – https://www.youtube.com/watch?v=cSwPOpOKr3w&t=312s

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami