UP | HOME
Sachin Patil

Sachin Patil

Free Software Developer | GNU Emacs Hacker

Introduction to Clojure
Published on Oct 18, 2013 by Sachin.

Clojure has been around for couple of years now. It has been recognized for its features like functional programming, elegance, lisp and JMV support. Unlike many popular dynamic languages, it is fast and written to take advantages of all optimizations possible with modern JVMs.

I personally like clojure because it is lisp. Lisp has very tiny language core with almost no syntax and has a powerful macro support. With this brief introduction lets start setting up Clojure.

lein

Before we start programming in Clojure, lets setup leiningen. Leiningen is for automatic Clojure project setup which will create all necessary file required for any Clojure project.

  • Download lein binary file from GitHub and copy it into the ~/bin/ directory.

    wget https://raw.github.com/technomancy/leiningen/stable/bin/lein
    
  • Add ~/bin PATH in the ~/.bashrc file

    export PATH=~/bin:$PATH
    
  • Make the binary executable

    chmod a+x ~/bin/lein
    
  • Finally run the self installer

    lein
    

Lein configuration

  • Lein version-2 uses the idea of profiles. The location of file is ~/.lein/profiles.clj. Create this file if it does not exist.
  • A sample profiles.clj file looks like below

    1: {:user {:plugins [[lein-difftest "1.3.8"]
    2:                   [lein-marginalia "0.7.1"]
    3:                   [lein-pprint "1.1.1"]
    4:                   [lein-swank "1.4.5"]
    5:                   [lein-exec "0.3.0"]
    6:                   [lein-ring "0.8.7"]]}}
    

You can see all the required libraries which are default to all Clojure projects.

Upgrade

  • To upgrade lein to its latest stable version use,

    lein upgrade
    

Project/App

Create a project or an app.

  • Create a project using

    lein new my-project
    
  • Create an app using

    lein new app my-stuff
    
  • Create a noir project(noir is a plugin for web framework)

    lein new noir my-web-project
    

REPL

REPL(Read-Eval-Print-Loop) can be your excellent interpreter or debugger while you work on a Clojure project. You can run REPL once you create a project.

  • Lets say the project name is my-stuff, go to project directory and run REPL using

    1: cd my-stuff
    2: lein
    
  • Inside repl prompt, run (-main)

    my-stuff.core=> (-main)
    
  • If you have done some change in a code, reload it

    my-stuff.core=> (require 'my-stuff.core :reload)
    
  • Run a program using lein (Outside the REPL prompt.)

    lein run
    
  • If you are happy with your app, package it into an executable jar

    lein uberjar
    
  • And run jar file using

    java -jar target/my-stuff-0.1.0-standalone.jar
    

Library

A Clojure library can be created in a same way.

Create a library using

lein new default my-lib

REPL

1: lein repl
2: ...

Simple queries can be performed inside a REPL

1: user=> (require 'my-lib.core)
2: nil
3: user=> (ns my-lib.core)
4: nil
5: my-lib.core=> (my-func 3)
6: 9

Dependencies

Compile

Compile your project in to an executable jar using

lein uberjar

Run

  • Run the standalone jar using

    java -jar target/my-stuff-0.1.0-SNAPSHOT-standalone.jar
    

Connecting to REPL server

  • A new REPL server is started at http://localhost:port when you invoke

    lein repl
    

    from a project directory.

  • You can now connect to existing server using

    lein repl :connect nrepl://localhost:PORT
    
  • for example

    lein repl :connect nrepl://127.0.0.1:37451
    

Generate documentation

  • Install marginalia

    1: cd ~/.lein
    2: touch profiles.clj
    
  • Add following line to profiles.clj (Your marginalia version may be different)

    {:user {:plugins }}
    
  • Then, in your project

    cd *path/to/project*
    
  • Install using

    lein deps
    
  • Generate docs

    lein marg
    
  • Browse docs in a web-browse file://path/to/my-proj/docs/uberdoc.html

Next (Updated on 03 Feb, 2016)

References