A Brief Beginner's Guide To Clojure

For a spartan yet still productive set-up to rapidly get you going, I recommend for now just using a text editor and a terminal window or two. Details below.

Text Editor Setup

If you’d like to use GNU Emacs, see my 10-minute Emacs + Clojure guide, or else the Clojure with Emacs guide at CDS.

If you’d like to use Eclipse or Vim, see their respective tutorials at CDS.

For an easy-to-use GUI editor, try jEdit as described in the Dev Tools guide at CDS.

Clojure Projects

Whenever you want to start writing a Clojure application or library, it’s most common to first create a new project for it.

Aside: You can also create a self-contained Clojure program in its own single source code file, like a shell script. That is covered later, in the Standalone Scripts chapter. For now though, we’ll discuss the customary way of creating a Clojure program, which is to create a project for it.

A project is just a directory (named after the project) which should contain:

  • a project.clj file,
  • a README.md file,
  • your source code, and
  • usually other sundry files.

The tool that’s used to create and manage Clojure projects is called Leiningen (“line-ing-en”), or just “lein” for short (which is also the name of the Leiningen command-line program). We’ll discuss lein in greater detail in a later chapter.

To start tinkering around with some Clojure code, we first need to install Leiningen and then use it to create a little project within which to work.

Create a ~/bin dir if you don’t already have one (and make sure it’s on your $PATH), and install lein like so:

cd ~/bin
wget https://raw.github.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
lein self-install

(Lein’s self-install will create ~/.lein and ~/.m2 directories.)

An app project example

Create a new application program / script project:

$ cd ~/temp
$ lein new app my-app
Generating a project called my-app based on the 'app' template.
$ cd my-app

Open project.clj in your editor and change the version string from “0.1.0-SNAPSHOT” to just “0.1.0”.

Open up src/my_app/core.clj as well.

Note: in your source code you use the hyphen (ex. “my-app”), but — due to the underlying Java platform — corresponding directory names below the project directory must instead use an underscore (ex. “my_app”).

The -main function is just what it looks like (your program’s main entry point). Back in your terminal, from your project directory, start up a repl and call -main:

$ lein repl
...
my-app.core=> (-main)
Hello, World!
nil

Over in your editor, change that “Hello, World!” text to something else and save the file. Now back in the repl, reload the file and run -main again:

my-app.core=> (require 'my-app.core :reload)
nil
my-app.core=> (-main)
Hello, You!
nil

Quit the repl using Ctrl-d. Now run your program like so:

$ lein run

When you’re happy with your app, package it up and copy the resulting jar file to your ~/bin directory:

$ lein uberjar
$ cp target/my-app-0.1.0-standalone.jar ~/bin

and run it:

$ java -jar ~/bin/my-app-0.1.0-standalone.jar
Hello, You!

You can now send that jar file to a friend and they should be able to run it likewise (as long as they have Java installed). :) To make it even easier to run your Clojure app, see distributing apps.

A library project example

To create a Clojure library:

cd ~/temp
lein new default my-lib   # Or `lein new my-lib` for short.

cd my-lib

In your editor, open src/my_lib/core.clj and between the (ns ...) and the (defn foo ...), add your own function:

(defn my-func
  "Docstring for my-func."
  [x]
  (* x x))

Save the file. Back in the terminal, start up a repl and try out that new function:

$ lein repl
...
user=> (require 'my-lib.core)
nil
user=> (ns my-lib.core)
nil
my-lib.core=> (my-func 3)
9
my-lib.core=> (doc my-func)
-------------------------
my-lib.core/my-func
([x])
  Docstring for my-func.
nil

In my-func, change the “*” to a “+”, then try out your changes:

my-lib.core=> (require 'my-lib.core :reload)
nil
my-lib.core=> (my-func 3)
6

A REPL without a project

Incidentally, anytime you want to fire up a repl — say, for reading docstrings or for just trying things out — you can do so outside of and without need of a project. For example:

$ cd ~/temp
$ lein repl
...
user=>

Workflow Summary

While you’re learning and experimenting with Clojure, your workflow will likely be something like this:

  1. open your project’s src/my_proj/core.clj in your editor
  2. cd into your project directory and start up the repl (lein repl)
  3. switch to one of the namespaces you’ve made (ex. (ns my-proj.core))
  4. interactively call various functions
  5. edit your code, then reload it: (require 'my-proj.core :reload)
  6. go to step 4