My New Chomma, Clojure

A few months ago I decided that it’s time to learn a new programming language that’s completely outside my normal frame of reference. After watching a video on InfoQ about the future of programming languages, I decided Clojure would make an interesting choice. I bought the Pragmatic Programmer book, and what can I say, we’ve been becoming good friends these days.

I think I’m taking to the functional programming paradigm, and like the idea of pure functions, without shared state. Even when I program C#, I tend to make heavy use of its closures like delegates, events, anonymous methods, and lambdas, using methods as data. Learning Clojure is quite a challenge if you’ve been conditioned by years of static typed, imperative programming.

A Very Basic Clojure Work Flow For Beginners

What I’ll describe here is a very basic workflow that will enable you to write some trivial Clojure apps and work through some of the examples you might find in books and on the web. Some of these tasks aren’t that clear reading the available material, as the authors usually skip to the more exciting parts of the language.

1. Get Counterclockwise for Eclipse
2. Create an executable command line script (in my case I called it to launch Clojure REPL (Read Evaluate Print Loop or Interactive Console) with the required libraries referenced in the class path (Clojure runs on the Java VM):

java -cp .:src:lib/JLine/jline-0_9_5.jar:lib/clojure-1.2.1.jar:lib/clojure-contrib.jar jline.ConsoleRunner clojure.main

This adds the necessary packages to the Java class path, and launches Clojure. JLine adds functionality to the Clojure REPL, like being able to press the up arrow to retrieve the previous command.

3. Compose your Clojure application by grouping related functions in the same namespaces. The first difference you’ll notice between Clojure, and an imperative language like Java or C#, is that it doesn’t have classes, only namespaces or packages. You define a namespace with the ns function:

(ns algorhythm.test.geometry.trigonometry-tests)

This tells Clojure to switch to that namespace, and if it doesn’t exist to create it, and to create all consecutive functions defined under it. To import another namespace to make its functions available you use the “(:use”:

(:use clojure.contrib.test-is algorhythm.geometry.trigonometry algorhythm.geometry.geometric-vector)

4. Start by writing some unit tests for your Clojure application. Or if you don’t do Test Driven Development (TDD), you can skip straight to 7, writing your actual implementation.

(ns algorhythm.test.geometry.trigonometry-tests
(:use clojure.contrib.test-is algorhythm.geometry.trigonometry algorhythm.geometry.geometric-vector))

(deftest find-longest-vertex-should-find-the-longest-vertex
(def triangle {:vertex-y (struct-map geometric-vertex :length 65 :link (struct-map vertex-link :angle 90 :vertex-name “vertex-x”)),
:vertex-x (struct-map geometric-vertex :length 99 :link (struct-map vertex-link :angle 20 :vertex-name “hypotenuse”)),
:hypotenuse (struct-map geometric-vertex :length 91 :link (struct-map vertex-link :vertex-name “vertex-y”))})

(is (= 99 (find-longest-vertex triangle))))

First create and switch to the namespace where we’re going to define our unit tests. Then we tell Clojure to reference the Clojure.contrib library, where Clojure’s unit test framework is located. You then declare your unit test functions, with (deftest …), and do assertions with (is …).

5. Launch Clojure’s REPL from terminal and load your unit test .clj files with (load-file …).

jacquesd@ubuntu:~> ./

Clojure 1.2.1
user=> (load-file “source/algorhythm/test/geometry/trigonometry_tests.clj”)

6. Run all loaded unit tests on the Clojure REPL with (run-tests). They will fail.

user=> (run-tests)

7. Now, write your required functions and repeat the cycle from 5. More specifically in our example, we should be writing algorhythm.geometry.trigonometry and algorhythm.geometry.geometric-vector, that’s required by our example unit test.

Okay, that’s my 2c to help your Clojure baby steps along. Preferably you’d opt for a proper project build system, like Leinigen, instead of manually loading and executing files through the Clojure REPL. But that’s a story for another day…


Discovering Shoal: The Java Clustering Framework

I recently discovered an open source clustering framework for the Java platform, Shoal. What interests me about the framework is that it provides programatically controllable clustering for Java applications. I am definitely considering Shoal’s Java clustering for the next version of GhostBlade. For a few months I have been working on my open source project, GhostBlade. GhostBlade is an open source Java e-commerce application, based on db4o as the database, and Google Web Toolkit for the user interface. It’s not available to the public yet, but should be within the next few months.

What bothers me a bit about db4o is that it is primarily targeted at embedded applications, and does not really scale well to enterprise size systems. But I love it, since I’ve reduced my database development time by about 30-50%, and do not have to do Object Relational Mapping (ORM). The reason I’m thinking of using Shoal, or a comparable clustering framework, is that I’d like to build an extremely scalable enterprise level e-commerce application based on db4o.

I was thinking along the lines of adding the capability to create a clustered application where the administrator can partition the application based on its classes, and functionality. For example he can command GhostBlade to store the Order class on node X in the cluster, or the search feature must be handled by node Y. Whether this will be possible with Shoal’s clustering is requires some prototyping research. Watch this space for more on the db4o-Shoal clustering stunt.