Moore’s law predicts that the speed of computers will increases by doubling the number of transistors on chip every two years. Yes, nowadays the computers can still get faster, but only if there are multiple CPU’s available. You need multi-threading techniques if you want to address all the CPUs. If I want to take advantage of multi-threading, then I should figure out how to apply multi-threading.

One of the major problems of multi-threading is that it’s hard to manage the state of variables between different threads. Concurrency in PHP is still not possible. So then I was trying to figure out how multithreading in C++ is applied. But it was hard to grok for me. The thought of eight, sixteen, or even more threads to run my application was too much to handle for me.

While I was wondering if there is a practical way to do this, I stumbled upon Clojure.

Clojure

Clojure is the new kid on the block. They said it’s designed for concurrency, which is nice if you’re working a lot with data. Lazy data structures are built-in so you can focus more on writing code than on performance tuning and efficiency. Also, Clojure is a functional language. This makes the path smooth to move functional programs into parallel multithreading systems. Clojure’s approach to paralellism makes it easier for me to write correct multithreaded applications.

Lisp nature

Clojure derives from Lisp, which is a functional, simple and elegant language. In the past I have written some Lisp libraries for Emacs.

In Lisp there is nearly no syntax. What I mean by that is that all structures look the same: the first item is a function and the rest are the arguments. And it removes the need for operator precedence rules. An example in Lisp:

Example in Lisp

We call everything between the parentheses a list. Evaluation proceeds from left to right. The first thing in a list is a function. So the plus-sign (+) is a function that receives the arguments, which are 1 and 2. That makes 3. So the (+ 1 2) will evaluate to (* 2 3). Thus makes 6.

Another example when defining functions in Lisp:

Greet

The structure seems simple? It is! From development I have learned it’s sometimes better to keep your work simple. That enables you to move rapidly with the work, into multithreading for example. The nature of Lisp fits well in that philosophy. Besides, its lambda calculus and macros are a nice addition that are unknown for most languages.

Java

When reading more about Clojure, it turns out Clojure sits on top of Java stack. So it can interoperate with Java with ease, but retains his Lisp data structure:

clojure=> (* 2 (+ 1.0 2))
6.0

In general, though, Clojure aims to preserve information like in Java. So adding two longs will return a long; adding a double and a long will return a double. Let find the type of number 6.

clojure=> (type 6.0)
java.lang.Double

The function type tells us that the argument 6 is the Double type. So most of the types in Clojure are plain old Java types. Which is good news, you get access to Java’s rich, mature API and the myriad of libraries that are implemented for Java in the past 15 years. You don’t need to reimplement lower level stuff (think of password hashing, digests, HTTP headers, sockets, etc).

Now you can express something in a few lines of Clojure. And then compile it to JAR-files, APKs for Android, running in the Java Virtual Machine (JVM), compile it to C#, etc.

To be frankly, the start-up time of the JVM isn’t the fastest. This makes Clojure not the ideal candidate for writing utility scripts. However, the JVM runs your code pretty fast if there is no need to reload (like server configurations). And you get garbage collection, JIT, uberjars, etc.

Webdevelopment

For interaction within the web browser, you need Javascript. If you want to do more complex interaction, you need many Javascript plugins and hella work with coding and testing (there is no suitable typechecking).

Now you can also compile Clojure to optimized Javascript. That makes the life easier, I will make demos of it soon.

Speed of development

Clojure is a pretty concise language. You need less time to write, so you can spent more time on designing structures. Furthermore, I’m really bothered when I encounter an unfamiliar bug. Makes you to spent time on debugging instead developing. With Cojure you have a REPL, which stands for Read Evaluate Print Loop. This makes development and debugging quicker and more fun. It also dramatically speeds up the learning process.

Conclusion

So far I found Clojure a pretty language. I like lazy sequences, immutable, concurrency and refs. But the community and ecosystem around are still a bit young and immature. Most of time I’m missing proper documention, so I find myself left to reading the source code. Good documentation is something I highly value. Even PHP knows better documentation.

But I will watch Clojure closely. If you want to read on, I found this article an interesting read: http://www.mkbergman.com/1825/why-clojure/