
Clojure Programming Cookbook
By :

REPL is the interpreter of Clojure, and it is an acronym of Read Evaluate Print Loop. Unlike other interpreter languages, such as Python or Ruby, Clojure REPL automatically compiles into Java's byte code after the reading expression. Then, REPL evaluates the expression and returns the result of the expression. This dynamic compilation feature of REPL makes Clojure code execution as fast as executing pre-compiled code.
Before you set up your Clojure environment, the Java Development Kit (JDK) is necessary. The JDK version should be 1.6 or later. Throughout the book, we will use JDK 1.8 to develop and test the code.
This is how the command-line result will look, once you type java -version
:
Leiningen is a standard build tool for Clojure. It simplifies the Clojure development, including setting up your project, compiling and testing code, and creating libraries for deployment.
It's easy to set up a Clojure environment using Leiningen. There are only a few steps before you can enjoy Clojure in REPL!
Here are the steps we need to perform to run Clojure REPL:
lein
script (or on Windows, lein.bat
).
$PATH
where your shell can find it (for example, ~/bin
):$ mv lein ~/bin
$ chmod a+x ~/bin/lein
lein
, and it will download the self-install package:
living-clojure
:$ lein new living-clojure
Here is a very simple code to demonstrate how REPL works. This code simply loops forever with read
, eval
, and println
functions:
user=> (defn simple-repl [] #_=> (try #_=> (while true #_=> (println (eval (read))) #_=> ) #_=> (catch Exception e (println "exited..")) #_=> ) #_=> ) #'user/simple-repl user=> (simple-repl) (+ 1 1) 2 (defn hello [s] (println "Hello world " s)) #'user/hello (hello "Makoto") Hello world Makoto nil exited.. nil user=>
You can exit simple-repl
by entering ^D
(Ctrl + D).
Leiningen is a very powerful tool for Clojure developers. The lein new living-clojure
command generates the following directory structure:
Let's pick up project.clj
, which defines the project:
(defproject living-clojure "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.8.0"]])
In project.clj
, the :dependencies
section declares the libraries used by your project.
Leiningen internally uses Maven to solve the dependencies of libraries. However, declaring libraries in project.clj
is much simpler than doing it in the pom.xml
file of Maven.
To use other libraries for your project, add them to the dependency section. We will review how to do this in a later recipe. In the preceding project.clj
file, the Clojure library named org.clojure/clojure
is declared and automatically downloads in the maven
directory. This is the reason why you don't need to download and set up the Clojure library explicitly.
Without Leiningen, you have to do it in a more native way. Here are the steps:
clojure-1.8.0.jar
.clojure-1.8.0.jar
using wget
:$ wget http://repo1.maven.org/maven2/org/clojure/clojure/1.8.0/
clojure-1.8.0.jar
$ java -jar clojure-1.8.0.jar Clojure 1.8.0 user=> (+ 1 1) 2
REPL supports the command-line editing feature, like Linux bash shell does, but the preceding way does not. Another difference is that REPL solves library dependency problems in project.clj
, but using the native way, you can solve them by yourself.
Please see related web sites as follows:
vi
user, try the fireplace plugin: https://github.com/tpope/vim-fireplace