Sunday, July 28, 2013

Clojure koans are cool

I found another post in the Unboxing series highlighting Clojure Koans.  These are simple learn-by-example snippets of Clojure code hosted by hosted at clojure-koans on GitHub.

While you can download the zipfile and get started with that, I decided to follow the advice on the GitHub homepage and clone the repository in case I wanted to contribute something later on.  Simple install steps (using Fedora 18):

> git clone https://github.com/functional-koans/clojure-koans.git
> cd clojure-koans
> git checkout -b joe   # create a branch "joe" and switch to it
> cd src/koans

In a 2nd window, execute:

> cd clojure-koans
> lein koan run          # assumes you have leiningen 2 installed

The koans are essentially unit-tests, and the lein command in the 2nd window will start an auto-runner, which will run the tests and stop at the first failing one.  You should immediately see the output:

Starting auto-runner...
Considering src/koans/01_equalities.clj...

Now meditate upon src/koans/01_equalities.clj:3
---------------------
Assertion failed!
We shall contemplate truth by testing reality, via equality
(= __ true)

which indicates the first koan/test is failing.  Edit the indicated file src/koans/01_equalities.clj and replace the double-underscore placeholder __ with the answer true, then save the changed file.  The auto-runner then advances to the next koan:

Now meditate upon src/koans/01_equalities.clj:6
---------------------
Assertion failed!
To understand reality, we must compare our expectations against reality
(= __ (+ 1 1))

As you go along, you'll get hands-on practice learning Clojure, and will probably get tripped up at least a bit which will make those details more real and easier to remember (the one with slicing a vector via subvec highlighted a bad assumption I was making).  Since you are making changes to the source code in your new branch joe, you can periodically check your work into git via:

> git status
On branch joe
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   src/koans/01_equalities.clj
#       modified:   src/koans/02_lists.clj
#       modified:   src/koans/03_vectors.clj
#
> git commit -am'First 3 koan levels completed'
<output elided>

The reason you created and worked in the Git branch joe is that you can now use "git pull" to grab updates from the clojure-koans repo on GitHub without getting conflicts in your completed koans.  You can do a git merge later if you wish.

Enjoy!
Joe Cooljure


Below I have copied the original Unboxing post in case it ever disappears from the internet:
--------------------------------------------------------------------------------

Unboxing: Clojure Koans

Overview
In our latest Unboxing post Principal Developer Gavin Davies explores functional programming using Clojure Koans.
Author
Gavin Davies
Date
01 May 2013

About Unboxing

Unboxing is our regular feature where someone at Box UK spends a few hours playing with a new framework, library, tool or technique and blogs about his or her experience. You can read more Unboxing features here.

Clojure Koans

Over the last 18 months there's been a growing buzz in the Box UK developer community about functional programmingWhile I’ve done a bit of “functional-ish” stuff in JavaScript and used closures and first-order functions, most of my experience has been in the Object Oriented world, so I decided to learn a bit more about the functional programming paradigm.
Having seen two of our coders give tech talks on Clojure, I had a good idea of what I was aiming at but still wasn’t sure where to start. Dayle Rees recommended Clojure Koans to me so I got stuck in!

What is it?

Clojure Koans is a series of tiny steps towards learning Clojure. You learn by doing - each step is a tiny problem and you tackle them one at a time.

Getting to grips

I installed Clojure with the usual “sudo apt-get install clojure” routine then installed Leiningen, and started the project with a simple:
lein koan run
This ran a program in my console that automatically runs the tests every time I change anything in the directory. The first run showed that a test was failing:
gavin@GAV-UBUNTU ~/koans [master] % lein koan run
Starting auto-runner...
Considering ~/koans/src/koans/01_equalities.clj...
Now meditate upon ~/koans/src/koans/01_equalities.clj:3
---------------------
Assertion failed!
We shall contemplate truth by testing reality, via equality
(= __ true)
All I needed to do was open the source files and fix the steps! I created a project using Eclipse’s CounterClockwise plugin and opened the specified file:
(/opt/BoxUK/koans/src/koans/01_equalities.clj)
It was clear straight away what to do:
  (meditations
    "We shall contemplate truth by testing reality, via equality"
    (= __ true)
    ...
Aware that Lisp languages use a kind of Polish prefix notation, I fixed the code to read (= true true) and immediately the koan runner updated my console with a new job for me:
Considering ~/koans/src/koans/01_equalities.clj...
Now meditate upon ~/koans/src/koans/01_equalities.clj:6
---------------------
Assertion failed!
To understand reality, we must compare our expectations against reality
(= __ (+ 1 1))
I carried on working through the examples and quickly felt like I was starting to get a feel for this unfamiliar language.

Going deeper

It’s pretty much essential to keep a REPL open at all times and to run the little fragments of code.
For example, I got stuck on:
  "Or use the names of existing functions"
  (= __ (map nil? [:a :b nil :c :d]))
So I ran in the REPL:
user=> (map nil? [:a :b nil :c :d])
(false false true false false)
This illustrates more clearly what is going on and allows me to tinker. Actually moving things around is, for me, a much better way of learning than trying to execute unfamiliar code in my head; then, when the syntax makes sense to me, I am better able to mentally execute it.
I found it started getting quite difficult around part 13 which meant I had to do a lot more research, but, thankfully, the Clojure docs are largely example-driven.
I’ve long been eager to get to grips with functional programming and Clojure Koans has been a great springboard for me. It hasn’t made me an expert overnight of course, but it has allowed me to start putting things together. It drove me to the Clojure documentation a fair few times, which is a good thing; this style of learning is not spoon-feeding but is a great way to take small steps towards understanding. It’s an approach I would definitely recommend for learning new languages and techniques.

Want to know more?

“Tell me and I forget, teach me and I may remember, involve me and I learn.” - Benjamin Franklin
The Clojure website is clojure.org
Clojure Koans is available at:

No comments:

Post a Comment