Getting Started With Clojure Koans
03 Nov 2016Clojure is a dialect of the Lisp programming language that runs on the on the Java virtual machine (JVM).
One advantage of running on the JVM: Clojure apps can leverage the vast library of Java apps that already exist. If a company has invested heavily in Java, they can bring Clojure into the mix without having to re-write their tried and tested Java code.
This post shows how to start experimenting with Clojure on Mac OS X.
Install Leiningen
Leiningen is a build automation tool for Clojure projects. It offers a quick way to install Clojure and related tools. To install Leiningen on OS X, use Homebrew.
~$ brew install leiningen
==> Downloading https://homebrew.bintray.com/bottles/leiningen-2.7.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring leiningen-2.7.1.sierra.bottle.tar.gz
==> Caveats
Dependencies will be installed to:
$HOME/.m2/repository
To play around with Clojure run `lein repl` or `lein help`.
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completion has been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/leiningen/2.7.1: 8 files, 14.7M
~$
To confirm leiningen
installation…
~$ which lein
/usr/local/bin/lein
~$
And to determine which version of Leiningen you have installed…
$ lein --version
Leiningen 2.7.1 on Java 1.8.0_101 Java HotSpot(TM) 64-Bit Server VM
$
Now that installation is complete, it’s time to enjoy the Koans.
Get and Run the Clojure Koans
The Clojure Koans were inspired by the Ruby Koans written by a stalwart of the software development community, Jim Weirich. To get the Clojure Koans, clone the GitHub repo where they’re located.
$ git clone git://github.com/functional-koans/clojure-koans.git
$
From there, you can cd
into the directory where you cloned the koans and run them.
$ lein koan run
Starting auto-runner...
Considering /Users/rth/Code/Clojure/clojure-koans/src/koans/01_equalities.clj...
Now meditate upon /Users/rth/Code/Clojure/clojure-koans/src/koans/01_equalities.clj
---------------------
Assertion failed!
clojure.lang.ExceptionInfo: We shall contemplate truth by testing reality, via equality
(= __ true) {:line 6}, compiling:(/Users/rth/Code/Clojure/clojure-koans/src/koans/01_equalities.clj:4:1)
The Assertion failed!
line tells us that Clojure is starting us off with a failing test. Some developers will leave a project with a failing test at then end of the day so that they have a clear place to start on the next day.
Every time you solve one of the riddles put forth by a koan, the corresponding test passes and you are directed to the next test. The Clojure Koans are a fun way to exercise and build Clojure muscles.
Running a Clojure REPL
What if you’re going through the Koans and you want to experiment a bit? Clojure’s read-evaluate-print-loop (REPL) is a quick way to try syntax on the fly. You can get to the REPL via Leiningen.
$ lein repl
nREPL server started on port 59750 on host 127.0.0.1 - nrepl://127.0.0.1:59750
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_101-b13
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (+ 2 2)
4
user=>
user=> Bye for now!
You can exit the REPL with CTRL + D
.
Conclusion
Next step: Let’s keep learning!