Subscribe to Wrapped in Parentheses        RSS Feed
-----

What is Clojure?

Icon 4 Comments
This was originally a reply to some people in a thread asking me to explain what Clojure was and give a "Clojure Rocks" speech. After I wrote this, I decided to put it here so that I could link to it rather than have to reiterate myself in the future for these sorts of questions. So here it is, in all it's glory. Enjoy


Clojure is a new language. It's a modern Lisp on the JVM (and the CLR).

The language is built for concurrency and features a software transactional memory model built into the language. Clojure is known for making concurrency-related tasks a total breeze.

Clojure is a functional programming language. Clojure offers pretty much everything you'd expect out of a functional language. Lambdas, closures, a wide variety of collection types and a huge sequence library for working with them. It also gives you plenty of the good of OOP while keeping out the bad. Clojure offers polymorphism through extremely powerful multimethods. The polymorphism that Clojure offers through multimethods, protocols, and records is more flexible than what you'd expect in most object oriented languages. Clojure doesn't shun object orientation as a whole, and takes what Clojure can benefit from. Immutability is the default in Clojure, but Clojure isn't a purely functional language. Rich Hickey recognizes that, while functional programming is good, purely functional programming isn't always best for every task. Some things just scream I/O and mutability, and Clojure provides ways to do these things and remain concurrent easily.

Clojure is a Lisp, which means that it has a macro system. While macros are a little difficult to understand/comprehend at first, they're extremely powerful tools to have at your disposal. Things that would have to be added by compiler writers in languages like Java can usually be added to Clojure by writing a macro. If Clojure is missing a feature that you want, you can quite literally write it yourself. Being a Lisp also means the syntax is very minimal. There isn't much to remember. The parentheses can be a little offputting for a while, but you eventually get used to them, and decent editor support can make them disappear entirely.

Clojure actually has less parens than other Lisps, and it uses stuff like curly brackets and square brackets for some things, where some other Lisps typically just have parens upon parens. For example, in Common Lisp, you would do this:
(let ((a 1) (b 2)) (+ a B)/>)
but in Clojure, you would do this:
(let [a 1 b 2] (+ a B)/>)
In Clojure, commas are whitespace, so you could even do
(let [a 1, b 2] (+ a B)/>)
to make it more readable if necessary.

Lisps look the way they do because they are homoiconic. There is no distinction between code and data in Clojure (or <insert Lisp here>), and Clojure applications are made up of Clojure data structures. This is an extremely powerful and equally hard to comprehend concept, but once you understand it, you'll understand why Lisp languages look the way they do. This may help.

Clojure can interop with other JVM languages. This means that you have the entire JVM at your disposal. You can interop with Scala, Java, JRuby, and Groovy all in the same application if you want (that would be interesting). Because of this, Clojure's age is rather irrelevant to it's practicality. While Clojure does have quite a few libraries written in it for it, Clojure also encourages direct interop with Java code when necessary, so even if there isn't a Clojure library for what you want to do, you can almost bet money that there will be one in Java that you can use.

Clojure's interop usually leads to code being smaller than the Java alternative, and I've heard some people half-jokingly say "Clojure does Java better than Java does Java" due to the way one uses Java code from Clojure. With Clojure's interop sugar, there are usually less dots and parentheses required to call Java code than if you were calling the same code from Java.

Clojure is dynamic. You can modify running code. If you have an application running, you can change pieces of that application and reload it without ever restarting the application. It's unbelievably useful.

Clojure is interactive. Clojure encourages interactive programming rather than a compile/run/crash/debug/repeat cycle. You can run code in the REPL and see it's results, load code into the REPL, and even run your applications in the REPL (in which case you have access to your entire application while it's still running, hence the "modify running code" stuff). It's an insanely productive way of doing things.

Clojure is fast. It can get close to, or reach the speed of Java with proper optimizations (type hints and such). Primitives support and such that is going into Clojure (1.3, I think) will make it even faster. Clojure encourages a "make it work, and then make it fast" philosophy, so the "making it fast" part usually only consists of tiny things like adding type hints that don't require you to really change your code much, or at all.

Clojure is concise. Not much to say here. With Clojure being dynamically typed (of course if your statically typed language has type inference, this is usually irrelevant), functional, and being a Lisp, code tends to be terse. Not so terse that it's unreadable (see the J programming language), but generally more concise than anything you'll see in Java. Code in Clojure is almost always much smaller than the equivalent idiomatic Java code.

Clojure has a very active and friendly community. The primary mailing list (a google group) is insanely active, and there are nearly 300 users in the #clojure IRC channel on the Freenode network at all times. More users join all the time. New libraries are released every day, and tools are getting better all the time.

Being 3 years old, IDE support isn't perfect yet. However, there are plugins for all the major Java IDEs, and they are functional. I don't use them so I can't vouch on the effectiveness of any of them. In Emacs, you can use Slime with the swank-clojure adapter, which will be familiar to Common Lisp users. If you've ever wanted to learn Emacs, Clojure is a good reason to do it. Emacs (SLIME, specifically) is really the most advanced development environment for Common Lisp or Clojure. Not telling you to ditch your IDEs for Emacs, but just encouraging you to try it out sometime. Especially if you aren't satisfied with IDE support in your favorite IDEs. There is also a popular VimClojure thingy for Vim that I'm unfamiliar with, but I know some people who use it. You Vimmers aren't left out. The plugins are all in active development, so expect improvement in the future if they don't meet your expectations now.

My resource thread has tons of links that should be useful for getting you started, so I wont reiterate them here. You can find more information about Clojure, including screencasts that have been known to sell Clojure better than I by Rich Hickey himself, at http://clojure.org.

If you have any other questions about Clojure (practice or theory), you can post them in the Functional Programming forum here on DiC (but I'll probably be the only one to answer them), or in the #clojure IRC channel on freenode. There is also the google group. While a little bit slower, you'll get a ton exposure there, and you should get an answer pretty fast anyway.

This was a public service announcement brought to you by Clj@DiC. If it wasn't what you expected, we'll write a macro to make it better. ;)

I hope this was enough to get some people interested, and I hope I've done Clojure justice.

EDIT: Fixed the screwed up casing in the code examples.

4 Comments On This Entry

Page 1 of 1

moopet Icon

09 August 2010 - 01:10 PM
It's made me interested. I'll make the time to check it out as soon as I've got through my current workload. Thanks for the intro :)
1

Raynes Icon

09 August 2010 - 09:30 PM
You're very welcome. I hope this post will be enough to pique the interest of lots of people. I can't wait for there to be Clojure questions in the Functional Programming forum. :)
0

Hoornet Icon

20 August 2010 - 06:13 AM
The CLR version is going very very slow. There is still no way to just download and use it. One has still to build the darn thing from start :(
It would be nice this version would get a but more love, since a believe a lot more people would use it then..
0

Tom9729 Icon

24 August 2010 - 07:07 AM
Any chance we can get a similar post on Erlang? ;)
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2014

S M T W T F S
 123456
78910111213
14151617181920
212223242526 27
28293031   

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories