Adventures in Clojure - Getting Started
Following Stu’s series on Java.next I decided to take a look into Clojure. Clojure is a pretty cool language. It’s a lisp dialect that runs on the JVM. This is cool for a couple of reasons. First, you get all the advantages of the jvm. Second, you get the advantage of interoperability with java libraries. I am not necessarily excited about the later, nonetheless, there are still advantages to having solutions to problems that you can use to quickly get things done.
Although clojure has a nice website with decent language descriptions and a helpful Wiki, getting started can leave you a little wanting. Here’s a more precise setup guide to get you going. I am using Ubuntu Hardy on my desktop PC so this guide will be geared towards it.
First, let’s install the necessary base libraries
% sudo apt-get install sun-java6-jdk maven2 subversion
Now let’s check out the clojure source and build it
% svn co https://clojure.svn.sourceforge.net/svnroot/clojure/trunk clojure
% cd clojure
% mvn install
When maven gets finished you should end up with a shiny new jar in the target dir of the clojure source directory. Now to test this out let’s try the following from inside the clojure directory.
% java -cp target/clojure-lang-1.0-SNAPSHOT.jar clojure.lang.Repl
Clojure
user=>
Now that we have the basics setup we need to get our environment setup for real programming. The first enhancements come to the repl. It would be nice to have command completion, parenthesis matching (this is lisp after all), history across sessions, and last but not least editor hooks. The wiki suggests both JLine and rlwrap. JLine is ok, but lacking in the ability to do command completion and some other goodies. That being said let’s install rlwrap so that we get the most out of our environment.
% sudo apt-get install rlwrap
Next we need to create a script to make launching the clojure environment a little bit easier. Here’a a script from the wiki to help us out.
#!/bin/bash
BREAK_CHARS="(){}[],^%$#@\"\";:''|\\"
CLOJURE_DIR=/usr/local/clojure
CLOJURE_JAR=$CLOJURE_DIR/target/clojure-lang-1.0-SNAPSHOT.jar
if [ -z "$1" ]; then
rlwrap --remember -c -b $BREAK_CHARS -f /home/abedra/.clj_completions \
java -cp $CLOJURE_JAR clojure.lang.Repl
else
java -cp $CLOJURE_JAR clojure.lang.Script $1
fi
As you can see I moved the clojure directory to /usr/local/
. I also had to modify the path to the .clj_completions file
. Just copy this script, save it, chmod +x
, and move it to /usr/local/bin/
. Make sure you properly modify the paths. Last, just go ahead a create an empty file at ~/.clj_completions
. We will populate that next. Let’s use this script to populate the .clj_completions
.
(defmacro with-out-file [pathname & body]
`(with-open stream# (new java.io.FileWriter ~pathname)
(binding [*out* stream#]
~@body)))
(def completions (keys (ns-publics (find-ns 'clojure))))
(with-out-file "clj-keys.txt" (doseq x completions (println x)))
Save this code as clj-completions.clj
. Now simply run the script.
% clj clj-completions.clj
Once it’s finished you’ll end up with a file named clj-keys.txt. Simply move that file to ~/.clj_completions
and you are ready to rock. Now let’s test out our new hotness. Once you invoke clj try typing in some code and pressing tab.
Now you are ready to get programming! Stay tuned for more adventures in clojure.