What's about Clojure?
There has been a lot of hype about Clojure lately. So I decided to check it out.
Clojure? What's that?Turns out Clojure (pronounced "closure") is a LISP dialect, and thus is primarily a functional language. This is an excellent start since I loved LISP back at the University (LISP was the language used for functional programming and artificial intelligence courses), mainly because it turned inside out how I used to think about programming. Besides, LISP syntax is so homogeneous and clean it's just beautiful. Some people might also raise the old "LISP = List of Insipid and Stupid Parenthesis"...
Another characteristic of Clojure is that it primarily runs on the JVM (there also Clojure implementations that run over .Net framework). The Clojure interpreter/compiler takes Clojure code and converts it to Java bytecode, which is then executed on the JVM. This allows for the same advantages as Java, like WORA (Write Once Run Anywhere). Also Clojure supports Java API, so you can use Java classes in Clojure code, as well as Java libraries (JARs). This gives Clojure an impressive set of APIs and libraries to work with despite being a relatively new language. The only con about being based on the JVM is that Java is object-oriented. This means interaction with Java classes from Clojure code doesn't look natural.
One of the biggest points in Clojure, one that everyone is hyping about, is immutability and concurrency. Immutability means there's no concept of variable. Given a block of data, this data cannot be changed. This might look strange and wrong at first, if not violent, for any programmer not familiar with this concept. But this immutability allows certain advantages, the most important being greatly simplified multi-threading. The rationale behind this is fairly simple: if a block of data cannot be modified, then concurrency is as easy as running the threads in the wild. No need to synchronize critical sections since there are none. You can of course "modify" data, but Clojure will copy this data for you instead of modifying the original target. Clojure does this very efficiently. For more details about immutable data, you can check here.
This features make Clojure and extremely interesting language to take a look at. And if you never tried functional programming, beware because this can blow off your mind (if you can take it, because some people strangely just hate functional programming with weird reasons... maybe because they can't grasp the rationale behind it? I don't know).
Giving it a goOn Ubuntu, installing Clojure is fairly easy. First install the Clojure development tools:
sudo apt-get install clojure
Then I suggest you install Leiningen, which to simplify is a make for Clojure (but with a far worse name). You can install it from the repository, but I found the version there to be pretty outdated (1.7 while latest as of this writing is 2.0.0).
- Download the lein script from the website.
- Move the script to your favorite location for executables (e.g. ~/bin or /usr/bin) and add it to your PATH as well for commodity. Make it executable (chmod +x lein)
- Launch lein for first time to download/install it.
- Now you can create the default project (a "Hello world" of course...) in the current directory by executing lein new app hello-world (hello-world being the name of the project, so you can change it to anything else).
- cd to the hello-world directory created by Leiningen and execute lein run to run the example project. it should display the typical "Hello world!" message. You're good to go :)