Clojure is a dynamic functional language for the JVM, recently released in version 1.0. Clojure offers a new set of programming techniques for robust code and rapid development. In particular, it has new solutions for multicore computing. Whether you make the shift to Clojure or stick to Java, learning about this new language will challenge your assumptions about the best way to design software.
Clojure is a new language for the JVM. Like Groovy, Jython, and JRuby, it offers dynamism, conciseness, and seamless interoperability with Java.
Clojure is a dialect of Lisp, recently released in version 1.0. Developers often dismiss Lisp as impractical, perhaps because of its distinctive syntax, its ascetic simplicity, or the academic uses it's often applied to. Clojure is set to break that curse. Rich Hickey designed the language to make it easy and practical to take on the same sorts of problems you handle with Java, more robustly and with less code.
Any new programming language, no matter how good, has to find its killer app to break through into widespread use. Clojure's killer app is parallel programming for multicore CPUs, which are now the major route to increased processing power. With its immutable datatypes, lockless concurrency, and simple abstractions, Clojure makes multithreading simpler and more robust than in Java.
I'll describe some of Clojure's distinctive features and show how applying lessons from the language can make your Java code more elegant and less buggy. I hope that you come away wanting to learn more.
Let's start with the simple function in Listing 1, which calculates the area of a circle.
(defn
circle-area [r]
(* Math/PI r r))
Clojure code looks quite different from Java code, for a simple reason. In Clojure, code is data; it is built from exactly the same lists and vectors as any other data structure. The consistent homoiconic syntax makes it easier for both programmers and programs to understand and manipulate the code.
So, the function definition in Listing 1 is nothing but a list (marked with parentheses), holding a vector (marked with square brackets) and another list. The list syntax in the first line defines the function. After the function name, a vector holds the parameters. And in the last line, the list syntax invokes the function of multiplication, applied to three operands.
Clojure's minimalist syntax becomes quite readable with even a little practice. Support for Clojure in all major development
environments -- including NetBeans, IntelliJ, and Eclipse, as well as vi and Emacs -- makes it even easier to handle. Figure
1 shows an example from VimClojure, where paired parentheses are matched by color. (The function extracts lower-case characters
from a string: (get-lower "AbCd") is "bd".)
In fact, because of its lightweight syntax, a Clojure program is often simpler than the equivalent Java. The Java getLower() function in Listing 2, for example, has twice the brackets and four times the code as the Clojure function.
If map.add creates a new map, how do I make a 1000-entry map?By Anonymous on September 1, 2009, 9:38 pmIf map.add creates a new map, how do I make a 1000-entry map? If I use a simple iteration to read lines from a file and add them to a map, do I end up with 1000...
Reply | Read entire comment
Is It Wise?!!By Anonymous on August 28, 2009, 8:00 amSeriously, if someone needs to ask if it is wise to invest time in learning a new language, he's already bound to be a far worse programmer than he could be. I probably...
Reply | Read entire comment
What about "Clean Code" && far from readableBy Anonymous on June 9, 2009, 11:36 amYeah, and I'm sure you guys' standard of "Clean Code" and code "readability" is the golden standard we all need to subscribe to and live by. We are all creatures...
Reply | Read entire comment
Is it Wise?By Anonymous on May 20, 2009, 4:21 amThe question is: is it wise to invest much effort in learning new syntax? Can this work with all existing java APIs?
Reply | Read entire comment
This article not for beginners, but audience unclearBy Anonymous on May 19, 2009, 9:20 pmNo offense, but you should not pretend that your article is accessible to those with no functional language background. Having used Lisp and Scheme in the distant...
Reply | Read entire comment
View all comments