Tuesday, June 17, 2008

Introduction to Clojure

What is Clojure? The rest of this series will answer that question in an easy-to-understand way. If that's good enough for you, skip down to Why Clojure?, where I talk about what I like about Clojure. If you want the shorter, more difficult explanation, though, wade through this: In one jargon-heavy sentence, Clojure is a general-purpose, dynamic, compiled lisp that targets the Java Virtual Machine and that provides strong facilities for functional and concurrent programming. Pretty dense. What does it mean?
  • General Purpose It's suitable to a wide variety of tasks. You might consider it for almost anything you'd use Java for.
  • Dynamic It shares many features, like dynamic typing, with Perl, Python, Ruby, and other agile languages. These traits make it easy to write programs quickly.
  • Lisp It's in the Lisp family of languages, and it shares much of the philosophy behind them. For example, Clojure programs are written in Clojure data structures, which enables a powerful macro facility.
  • Compiles Although Clojure is dynamic, it is not interpreted. Instead, each expression and function is immediately compiled to Java byte code. Often functions written in Clojure run just as fast as their Java equivalents, especially when you give the compiler hints about what kind of values your variables refer to.
  • Java Virtual Machine Clojure runs on the JVM, and it can use the standard Java library, as well as all of the third-party libraries available for Java.
  • Functional Although you can access mutable Java types, Clojure has an array of immutable, fast data structures suitable to functional programming. This is a style of programming that emphasizes functions that don't change their input and programs that are composed of many small functions, glued together.
  • Concurrent Clojure includes a robust implementation of software transactional memory (STM) and reactive Agents. When combined with functional programming, these systems make it easy to write clean, correct concurrent programs.
Why Clojure? For me, Clojure hits a sweet spot. I like the power and flexibility that dynamic languages—especially lisps—provide, but I also want the speed, the library, and the community of a behemoth like Java.
  • The interactive development model it shares with Python, Ruby, and especially lisp makes it an excellent platform to experiment on.
  • Unlike most current dynamic languages, it lets me take advantage of multiprocessor machines across a variety of OS platforms.
  • It gives me access to the zillion libraries available for Java, and to the JIT capabilities of the JVM.
  • It provides speed in both development and execution.
  • I can deploy it wherever the JVM is available.
In a nutshell, Clojure lets me do interesting things quickly, without having to fight with it. I think you'll like it too.

2 comments:

Anonymous said...

cool, can't wait to try it!

Anonymous said...

It seems interesting also for me, java/lisp combination :)