Get Started With Clojure in 9 Minutes

Functional Works
5 min readAug 13, 2015

--

Last Monday I attended London Clojurian’s USwitch Dojo, where I worked in a group replicating the Dojo’s Project Board in Datomic. It was a great learning experience.

But before I could take part, I had to set up a Clojure development environment on my laptop, a Chromebook Toshiba 2 (which runs Linux via Crouton + Xiwi). It turned out to be a relatively painless process, taking a couple of hours, I even managed to pick up Emacs!

But YOU can start coding in Clojure much faster. 9 minutes to be exact. Just follow the guide below.

Clojure on a Chromebook: Emacs and Cider

What is Clojure?

Clojure is a programming language created by Rich Hickey, that was released in 2007.

It is:

  • A dialect of Lisp

= Powerful with many, many brackets

  • Hosted on the Java Virtual Machine (JVM)

= Parasitises Java’s stability/performance and can use Java libraries

  • Dynamically compiled

= Faster than scripting languages. Turned into Java byte code for the JVM

  • An impure functional programming language

= Does not enforce the functional paradigm

  • Aimed at Concurrency / Asynchronous Programming
  • Currently at version 1.7

Rich Hickey, creator of Clojure and Datomic

For an explanation of Concurrency / Asynchronous Programming see It’s Future Ready.

Need to Know

To install Clojure, you are going to need the Java Development Kit (JDK) and Leiningen.

The JDK is just Java for programmers. It is likely you already have it.

Leiningen is a build tool for Clojure. Build tools automate some of the development process for you. In the case of Leiningen it takes over the creation of new projects, running Clojure code and using the REPL.

For example, instead of typing in:

 clojure testcode.clj

Inside the project directory you would type:

lein run

Leiningen has a glorious mustache

Using the REPL (Read Evaluate Print Loop) is known as dynamic development. It is an interactive development environment, where you can test lines or sections of code before writing it into your project.

Installation

These are Linux Commands, and may be different on your Operating System.

Open your terminal / shell / command line.

Check whether you have Java 1.6 or higher:

java -version

If you don’t have Java installed:

sudo apt-get updatesudo apt-get install default-jdk

Go to here. Copy all of the text.

In your terminal, change directory to your /bin.
cd .. up twice, through home into / . Use pwd to locate yourself

ls looking for bin

cd bin

Open your text editor of choice, create a new file. Paste the copied text into the file and save it in /bin as lein. (Save As, go up from your user folder > home > /, to find it).

Go back to your terminal. Still in the /bin directory type:

chmod 755 lein

This makes the lein file executable. To install Leiningen type:

lein

If nothing runs, type:

lein self-install

Now cd to your default directory, and go to your normal code folder. E.g.

cd /Documents/code

Create a new project, called speedyclojure:

lein new app speedyclojure

Move through the newly created project:

cd /src/speedyclojure

Typing ls should show your new Clojure file:

core.clj

Open core.clj with your favourite text editor.

It will show the text displayed above on the left.

Now open a new terminal window, and travel to your ~/src/speedyclojure/ folder. Type:

lein repl

This will open the REPL in the terminal window, and in the current name-space.

lein repl can be run from any directory, but unless it finds a Clojure project, it will be a generic REPL with no loaded Clojure files.

In the REPL type:

(+ 2 2)

See, an interactive development environment.

Run the REPL split screen with your already open core.clj.

Back to your terminal window still in your /speedyclojure directory. Type:

lein run

You should see:

Hello World!

Congratulations! You have successfully installed Clojure via Leiningen.

Leiningen approves

Writing Functional Code

While you are still excited, above your -main function, type:

(defn hello [name] (str “Hey Hey “ name “. Hey. Hey.”))

Now change your -main function to:

(defn -main [who] (hello who))

Save your file. Go back to the /speedyclojure terminal. Type:

lein run “Ronda”

There should be no output (and no errors). It wouldn’t be very Functional if it printed out now would it?

To check it’s ouput, run it in the REPL. Type:

(-main “Ronda”)

You should see:

“Hey Hey Ronda. Hey. Hey.”

The function has to be above -main because the order of functions matters in Clojure. Your program is evaluated from the top, down to the bottom. See this stackoverflow answer for more information.

Congratulations! You have created your first Functional function

Yay Clojure

Not fast enough? Clojure in 19 Seconds

Go to http://www.tryclj.com/.

Type in:

(* 5 5)

Type in:

(def hello (fn [] “Hello world”))

Type in:

(hello)

You’ve guessed it. This is a REPL hosted online. Whenever you want to try out some Clojure code, but are outside your brand new Clojure development environment, just visit here.

How fast was that?

Well Done

You got to the end and jumped feet first into Clojure. Pat yourself on the back. You can now play around with the REPL and implement programs, have a look at Clojure Koans and 4Clojure’s Problem List for ideas.

Whatever you do, enjoy yourself, Clojure is great fun.

Next Steps

Learn to use Emacs and Cider. Taking it slowly, you can pick it up in 2 to 3 hours.

  • It will help with the many, many brackets you will use.
  • You can keyboard shortcut code into the REPL.
  • You will become more productive, as you progress with it.
  • Plus others programmers will be impressed.

Read more on Leiningen here.

Check out Cognitect.

Have a look at Clojurians to Follow on Twitter.

If you are in London, come down to the London Clojurian’s Dojos.

Thanks for finishing the post. If you liked it, follow us on twitter or sign up for our newsletter to be kept up to date.

If you would like a career in Functional Programming, check out our new job board.

If you have any feedback, thoughts or ideas for future posts contact us @Functionalworks.

--

--

Functional Works

Discover the best Functional programming opportunities through our AI Recruiter www.functionalworks.com