3 Replies - 10482 Views - Last Post: 15 August 2010 - 12:46 AM

Week #29- Clojure

Posted 12 August 2010 - 07:06 PM

This challenge should bring us up to date on challenges. This challenge on Clojure was submitted by Raynes.

CHALLENGE TITLE: Embrace Clojure

CHALLENGE: Use Clojure to write any sort of application you like. Use it to write a web application, a desktop application, or anything in between. Use some of your existing Java code in Clojure, or create a clone of one of your Java apps in Clojure. Have fun.

Clojure is an amazing new language that was created two years ago by a man named Rich Hickey. It is not an implementation of an existing Lisp, but a whole new Lisp in itself. It isn't held back by the limitations of the Common Lisp standard. It embraces the JVM to offer direct and idiomatic interoperation with Java (or any other JVM language), so you can use any existing Java code in Clojure, without wrapping it in a Lispy wrapper.

Here is what is written on the front page of clojure.org:

"Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR ). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime. Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.

Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.

I hope you find Clojure's combination of facilities elegant, powerful, practical and fun to use."

Clojure is the fastest growing language I've ever seen. It has gained a large following in just two years of development. The language has a book called Programming Clojure written about it, and three other books are in the works (one of them will be finished in February). If Clojure can do everything it has done in two years, I feel this language has an amazing future ahead of it.

RESOURCES:

There are lots of resources right at the homepage at http://clojure.org/, including API documentation, very good screencasts, a list of libraries, a cheatsheet, and various other general information about Clojure.

There is a relatively stable Eclipse plugin in active development, here: http://code.google.c...ounterclockwise, though it doesn't offer auto-indentation yet. I've been bugging Laurent about that lately, so it shouldn't be too long.

There is also an IntelliJ plugin you should be able to google to find.

By far, most Clojure users use Emacs coupled with clojure-mode and swank-clojure and Slime as their development environment, including myself. You'll get the most out of Clojure by familiarizing yourself with Emacs a bit, and setting it up with Slime as detailed on the swank-clojure github page, but you don't have too. I do strongly recommend it.

There is a very comprehensive Clojure tutorial here that some say is on par with the Programming Clojure book: http://java.ociweb.c...re/article.html I've not read it, though I think I will when I get a chance. I'll point out that some things are probably a bit outdated, but as long as read changes and such after reading through the tutorial, you should be fine. Programming Clojure is a little outdated as well, but luckily, a ton of more books are being written. For instance, Clojure has protocols (which are like interfaces in OOP), and types which are kind of like C structs. Types created with deftype are making Clojure's struct-maps obsolete, so if you're using Clojure from it's master branch, where deftype and defprotocol now exist, remember to use deftype instead of structs. If you are going to use the last major release (Clojure 1.1), deftype and defprotocol don't exist and struct usage will be fine. Just a little tidbit of knowledge for yas!

I'll note with enthusiasm that you do not have to know Java in order to learn Clojure. Yes, it definitely helps, and it helps even more if you know your way around the Java API, but it isn't necessary. As a matter of fact, learning and using Clojure can expose you to Java a bit, and make it easier to find your way around if you learn Java in the future. Since Clojure can directly access Java code, you have the entire Java library at your disposal. Where Java isn't broke, Clojure doesn't fix it.

Beyond what I've explicitly mentioned, I encourage you to explore the Clojure website I linked first; it has tons of good information.

This depends on what editor you decide to use. There is really no point in putting getting started information here. If you use Emacs with Leiningen (or maven) as your build tool, this will be self explanatory. Leiningen (or maven) will handle dependencies for you, even Clojure and the companion group of libraries, clojure-contrib. Otherwise, you'd just want to slap whatever Clojure version you want on the classpath (you probably know how to do this in the popular Java IDEs) and start coding.

IDEAS:

Write a tool you can use for your Java development.

Plan to write something in Java? Write it in Clojure instead!

For more advanced users: Think of a language feature you wish Clojure had, and then write that feature as a freakin' macro!

Also, I wrote a Clojure resource thread here: http://www.dreaminco...sources-thread/, it has links to stuff to help you get started in pretty much any development environment you could possibly want to use, and other fun stuff.

The book situation has drastically improved since I wrote this as well. You wont want to get Programming Clojure anymore. It was written for Clojure 1.0, and we're almost at the 1.2 release (RC3 at the moment). Right now, there are two books that haven't been printed yet and two that have been printed. The two that have been printed are Programming Clojure and Practical Clojure. If you want to get a dead-tree book, you'll want to get Practical Clojure. It's recent and pretty up-to-date. The other two are Clojure In Action and The Joy of Clojure. The Joy of Clojure is going to be the book on Clojure. It's actually complete, but needs technical review and wont be available in print until November. However, if you purchase the book through the Manning Early Access Program, you can buy the book (either ebook only or ebook and print), and you will receive frequent updates with the new versions of the book. This means that you can get the entire book (soon, 12 chapters at the moment) and updates with future versions of the book up until the book is printed. When the book is printed, you'll be sent the completed book (ebook AND the printed book, if you so desire).

If you want to learn about Clojure deeper than the scope of the challenge calls, and want to use it outside of this challenge, I highly recommend you preorder The Joy of Clojure. It's an amazing book.

It's an experimental pastebin-like site that I'm working on. While getting more web development experience and learning is a huge motivation behind this, it is a serious project. Another big motivation is that there aren't really any pastebins written in Clojure, so this one is being written in Clojure with an emphasis on Clojure.