preload
Nov 30

clojureThe concept of non-Java languages on the Java Virtual Machine is not new. The JVM has played host to several ported languages such as Ruby (JRuby), Python (Jython) and even Javascript (Rhino) along with languages designed primarily for the JVM like Scala and Groovy. Clojure (pronounced like closure) is a general purpose programming language, hosted primarily on the JVM.

There are four main aspects to the Clojure programming language,

  1. Clojure is a Lisp
  2. Clojure is a functional language
  3. Clojure plays nicely with Java
  4. Clojure has built in support for concurrency
Lisp, the programmable programming language

Lisp is a homoiconic language, i.e. a language wherein the primary representation of a program is also a data structure. This is the source of Lisp’s (and Clojure’s) “weird” syntax and of its true power. In Lisp, code is data and data is code.

In Clojure, code is made out of lists

(+ 1 2)

=> 3

(* 1 2)

=> 2

(first ‘(1 2 3 4 5))

=> 1

(first ‘(+ 1 2))

=> +

Homoiconicity in Clojure allows you to extend the compiler itself using a Macro. Language features like “for” and “and” can be implemented as a Macro instead of a language primitive.

Functional programming for the rest of us

In a function programming language, programs are executed by evaluating expressions. This is very different from imperative programming where programs are composed of statements which change global state when executed. Functional programming, on the other hand, avoids using state and mutable data.

In a functional language, functions are first class, which means that they are treated like other values and can be passed as arguments to other function. Functions can also return functions as arguments and functions which do are called “Higher-Order functions”

A functional program is often shorter and easier to understand than an imperative solution to the same problem. Another huge bonus is the ease of unit testing and debugging. Since, functions do not modify state; a function’s return value depends entirely on the arguments passed to it.

Clojure is a functional programming language. It provides the tools to avoid mutable state, provides functions as first-class objects, and emphasizes recursive iteration instead of side-effect based looping. Clojure is impure, in that it doesn’t force your program to be referentially transparent, and doesn’t strive for ‘provable’ programs. The philosophy behind Clojure is that most parts of most programs should be functional, and that programs that are more functional are more robust.

Java Interop

Clojure is always compiled to JVM bytecode, either at runtime or ahead of time and provides full access to the Java APIs. Java arrays are supported natively as well as all java primitive types.

(System/getProperty “java.vm.version”)

=> “14.0-b16″

Math/PI

=> 3.141592653589793

Clojure has syntactic sugar to make calling Java from Clojure very intuitive

Parallel

Clojure simplifies multi-threaded programming in several ways. Besides lists, Clojure also has as core data structures, immutable Hash-Tables and Vectors. Because the core data structures are immutable, they can be shared readily between threads.

It is often necessary to have state change in a program however and Clojure, being a practical language, allows state to change but provides mechanism to ensure that, when it does, it remains consistent, while alleviating developers from having to avoid conflicts manually using locks etc.

The software transactional memory system (STM) supports sharing changing state between threads in a synchronous and coordinated manner. The agent system supports sharing changing state between threads in an asynchronous and independent manner. The atoms system supports sharing changing state between threads in a synchronous and independent manner. The dynamic var system supports isolating changing state within threads. In all cases, Clojure does not replace the Java thread system, rather it works with it.

In a multicore world, programming languages like Clojure are a step in the right direction.

Real World Clojure

Clojure is a young language (recently celebrated its second birthday) and doesn’t have a large number of applications in live production. This doesn’t mean the language is not being used. The web framework, Compojure, has been written in Clojure and Clojure-Contrib, which is a user contributions library for clojure, has active growth.

In Conclusion

In spite of its age, Clojure’s popularity is visibly increasing. It is a true modern language and worth a look from more than just an enthusiast.

I recommend checking out the Clojure Book for a feel of the language and Paul Graham’s essays for a more in-depth understanding of the philosophy and history of one of the oldest and newest programming languages of all time.

About the Author

sidhant-godiwalaSidhant Godiwala is a graduate student in computer science at Nowrosjee Wadia College, Pune, India and an intern at hover.in. He has a passion for programming and an interest in Martial Arts and Game Development. He claims Paul Graham’s essays are what introduced him to Lisp and drew him into the world of functional programming. Sidhant is currently working on implementing a push-framework other other things in Erlang at hover.in. You can visit the hover.in devblog or his blog at ‘The philosophy and the craft

Written by Content Team on November 30, 2009     Print Print

Tagged with:

blog comments powered by Disqus

© 2004-2009 Rightrix Solutions