Cogs and Levers A blog full of technical stuff

Clojure and Leiningen quickstart

Introduction

Clojure is the modern LISP. Clojure is an elegant, dynamic programming language that runs a-top the JVM. In today’s post, I’ll show you how to get started with Clojure & Leiningen.

Getting installed

I’ve written this article from the perspective of a Debian user. Translating these steps into your own native environments should be as easy as re-structuring the installation steps to target your package manager of choice. Installing Clojure & Leiningen was a simple as this:

$ sudo apt-get install clojure leiningen

You’re done. You should now have Clojure and Leiningen at your disposal.

Initial steps

You’ll want to kick the tires on this puppy, so from your bash prompt fire up the REPL environment and try a few commands:

$ clojure
Clojure 1.2.1
user=> (+ 2 4)
6
user=> (print "Clojure is installed!")
Clojure is installed!nil
user=> (if (= 1 1) (print "Yes, 1 does equal 1") (print "Mathematics just stopped working"))
Yes, 1 does equal 1nil

Alright! Enough of this already. Let’s generate a project. Leiningen is a painless way to get started on your Clojure project. All you have to do, is issue the following commands and you’ve got a project ready to go:

$ lein new projname

Where projname is the name of your project. For this test, I just called mine “myproj”. If you have a look inside the directory that Leiningen has just generated for you, you’ll see the following sub-directories:

lib         - holds your programs dependencies
project.clj - a clojure file describing your project 
README      - duh! 
src         - your source files 
test        - any tests for your application

This is a pretty neat-and-tidy layout, ready for you to start coding.

Bulding and running and cleaning, oh my!

Leiningen also makes it very easy to build, run and cleanup your project. Here’s how. From within your project directory:

# Build your project
$ lein compile

# Clean any built files
$ lein clean

# Run your project
$ lein run

Too easy.