Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clojure Programming Cookbook

You're reading from   Clojure Programming Cookbook Handle every problem you come across in the world of Clojure programming with this expert collection of recipes

Arrow left icon
Product type Paperback
Published in Oct 2016
Publisher Packt
ISBN-13 9781785885037
Length 618 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Nicolas Modrzyk Nicolas Modrzyk
Author Profile Icon Nicolas Modrzyk
Nicolas Modrzyk
Makoto Hashimoto Makoto Hashimoto
Author Profile Icon Makoto Hashimoto
Makoto Hashimoto
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Live Programming with Clojure FREE CHAPTER 2. Interacting with Collections 3. Clojure Next 4. File Access and the Network 5. Working with Other Languages 6. Concurrency and Parallelism 7. Advanced Tips 8. Web Applications 9. Testing 10. Deployment and DevOps

REPL up!

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.

Getting ready

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:

Getting ready

How to do it...

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:

  1. Download and set up Leiningen from http://leiningen.org/.
  2. Download the lein script (or on Windows, lein.bat).

    How to do it...

  3.  Place it on your $PATH where your shell can find it (for example, ~/bin):
    $ mv lein ~/bin
    
  4. Set it to be executable:
    $ chmod a+x ~/bin/lein
    
  5. Run lein, and it will download the self-install package:

    How to do it...

  6.  Create a new project and go there. Using Leiningen, you can create a project from a project template. This example creates a project called living-clojure:
    $ lein new living-clojure
    

    How to do it...

  7.  Run REPL and put Clojure code into it:

    How to do it...

How it works...

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).

There's more...

Leiningen is a very powerful tool for Clojure developers. The lein new living-clojure command generates the following directory structure:

There's more...

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:

  1. Download clojure-1.8.0.jar.
  2. Download clojure-1.8.0.jar using wget:
    $ wget http://repo1.maven.org/maven2/org/clojure/clojure/1.8.0/
            clojure-1.8.0.jar
    

    There's more...

  3.  Run Clojure and test the Clojure code:
    $ 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.

See also

Please see related web sites as follows:

You have been reading a chapter from
Clojure Programming Cookbook
Published in: Oct 2016
Publisher: Packt
ISBN-13: 9781785885037
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime