Monthly Archive:: March 2010

Programming Clojure with Clojure 1.2.0 snapshot

This post contains a list of changes between Clojure 1.1.0 and 1.2.0 that would affect you if you’re reading Stuart Halloway’s “Programming Clojure”.

It looks like you’d have to replace,

(use ‘[clojure.contrib.str-utils :only (re-split)])
(filter indexable-word? (re-split #”\W+” “A fine day it is”))

with


(use ‘[clojure.contrib.string :only (split)])
(filter indexable-word? (split #”\W+” “A fine day it is”))

–> (“fine” “day”)

since the str-utils module got renamed to string, and the re-split function to split.

BeautifulSoup on Cojure? Enlive

I was looking for a suitable library for Clojure that would work like Python’s BeautifulSoup or lxml – and found enlive.

An excellent tutorial here http://github.com/swannodette/enlive-tutorial.

Clojure AOT compilation tutorial

I was trying to figure out how to AOT compile a Clojure program, in order to really see some fast execution times. The simplest way to describe AOT compilation would be how its done in Java,

javac file.java
java file

The invocation of the Java compiler (javac) is the pre-compilation of the source file, which is then loaded by the JVM in the next step. In the case of Clojure, when a program is run using,

clj myfile.clj

The code is first compiled, and then executed – resulting in large amounts of time for the output to be displayed even for simple programs.

The AOT compile process turned out to be trickier to set up than I expected, so I thought I’d put it out there for all those who get stuck after reading the original documentation at http://clojure.org/compilation.

Firstly, the directory structure for my experiment looks like this,

Directory structure

I created dir1, and its subdirectories as below. The code is in clojure/examples/ and the classes/ directory is the default compile.path – something that the documentation neglected to mention. Without this path, compilation WILL fail.

/dir1/.clojure
/dir1/clojure/
/dir1/clojure/classes
/dir1/clojure/examples
/dir1/clojure/examples/hello.clj

The .clojure file

This file is used with my clj script, that can be obtained from here.
It contains a list of directories that are to be specified to the Clojure compiler at compile time. The file looks like,

/dir1:/dir1/classes

hello.clj

This code of course is the default from the Clojure website, as a test.

(ns clojure.examples.hello
(:gen-class))

(defn -main
[greetee]
(println (str "Hello " greetee "!")))

The next step is to invoke the Clojure REPL using the clj script, from the dir1 directory.

Type in the following to compile the program in the clojure.examples namespace,

Clojure 1.2.0-master-SNAPSHOT
user=> (compile 'clojure.examples.hello)
clojure.examples.hello
user=>

Success! And the resulting output of the classes directory is,

$ ls /dir1/classes/clojure/examples

hello$_main__5.class
hello$loading__4946__auto____3.class
hello.class
hello__init.class

And lastly, to run this program as any other Java program, you can use,

java -cp ./classes:/opt/jars/clojure.jar:/opt/jars/clojure-contrib.jar clojure.examples.hello Viksit
Hello Viksit!


Also, I highly recommend Stuart Holloway’s book “Programming Clojure”. Its turning out to be an excellent read.