Setting up your environment to work with Clojure


Hi everybody,
in the last tutorial I’ve talked about Leiningen. Now the time has come to go deeper, learning how to work with Clojure with ease.

The mindless solution: Eclipse + counterclockwise

Eclipse is a general purpose IDE focused on JAVA, but you can easily customize it working with Clojure: read the tutorial here and you should be able to download and install the counterclockwise plugin. What you obtain will be an environment with syntax-highlight, REPL and the possibility to create and manage Clojure project like the Java ones.

Pros:
  • Easy to install/manage/update
Cons:
  • Even if you want to eval a single statement you have to start Eclipse

Emacs + SLIME + Swank

For someone Emacs is an extremely unfriendly editor, full of macros and shortcuts, for others is THE editor. I think that Emacs is a fantastic editor if you know how to work with it. In this part of the tutorial I’ll show you how to set up a working environment under Emacs where you can even eval Clojure code directly. If you are wondering about what is SLIME, you can simple image SLIME like your REPL. Swank is “a server that allows SLIME (the Superior Lisp Interaction Mode for Emacs) to connect to Clojure projects. To use it you must launch a swank server, then connect to it from within Emacs”. In a nutshell through Swank we can start a REPL within Emacs, eval our code snippets and much more. Before getting our hands dirty, we need to install ELPA, the Emacs Lisp Package Archive. Through ELPA we can install all the stuff needed with ease.

Let’s go!

The first thing to do is open Emacs and select the *scratch* buffer from Buffer menu (Buffer -> *scratch*) and paste the following code:

(let ((buffer (url-retrieve-synchronously
	       "http://tromey.com/elpa/package-install.el")))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)
    (eval-region (point) (point-max))
    (kill-buffer (current-buffer))))

Now bring the cursor just after the last parenthesis and press C-j (control + j) or manually select the command with Lisp-Interaction -> Evaluate and Print and wait for the operations to complete. Now close and reopen your Emacs and open a minibuffer with Alt+x or ESC+x. You will see the lower part of the Emacs window labeled with M-x.
Type


package-list-packages

inside the box and press ENTER. The window will split up in two frames and you’ll see a list of packages. Navigate through each row, bring the cursor over each of the following packages and (one by one) press “i”. You will see a “I” (a.k.a Installing) beside each package name:


clojure-mode
slime
slime-repl
swank-clojure

Now press x (a.k.a eXecute) to install the packages. If something fails, retry opening the minibuffer and tying package-list-packages again. The packages installed will be displayed red. You need at least the first three. Now you have syntax-highlight and your REPL, but you can’t use it yet.

Putting all together

The last thing to do is creating a Leiningen project (do you remember my last tutorial?) specifying like a development dependency swank clojure. So, remembering some Leiningen commands we create a simple general purpose Clojure project to play with:


lein new clj-on-fly

Now open the project.clj file and make it looks like this:


(defproject clj-on-fly "1.0.0-SNAPSHOT"
    :description "FIXME: write"
    :dependencies [[org.clojure/clojure "1.2.0"]
                   [org.clojure/clojure-contrib "1.2.0"]]
    :dev-dependencies [[swank-clojure "1.2.1"]])

Now within the project directory execute

lein deps

in order to download swank clojure into the project lib directory. Now you are ready to start a swank server, and you can do it within emacs pressing ESC+x and typing “shell” inside the minibuffer.
To try it out, you can open the “core.clj” file inside your project with Emacs, now create a new shell and type


lein swank

You should see and output like this:


user=> Connection opened on local port  4005#<ServerSocket ServerSocket[addr=localhost/127.0.0.1,port=0,localport=4005]>

Now return to your core.clj buffer, open a minibuffer and type:


slime-connect

press always “y” to the questions and finally you should see your interactive REPL, which you can query and play with, as well as evaluating all the code or even a snippet. Enjoy!
I hope you have liked this article.

Bye!

Alfredo Di Napoli

One thought on “Setting up your environment to work with Clojure

  1. Note to me: You can eval a single statement putting your cursor just after the last parentesis and selecting “eval last expression”. You will see the output of the evaluation on the lower frame and you’ll be able to test your functions into the slime-repl!

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...