Clojure for dummies: a kata
Join the DZone community and get the full member experience.
Join For FreeClojure is a LISP dialect; if you have ever been forced to use LISP in a computer science class, I understand your resistance towards this class of totally functional languages.
In fact, LISP for many of us means lots of Reverse Polish Notation where (+ 1 2) evaluates to three; absence of for cycles and other commodities to privilege tail recursion; and absolute lack of state in the form of variables (the state isn't really absent: I've seen it on the stack.)
However Clojure is a general-purpose language and has captured the attention of great minds such as Uncle Bob, who goes as far as suggesting it should become our only standard language.
A known fact about Clojure is that it can compile to JVM or CLR bytecode, which means it can be deployed to the same platforms as Java or C# applications. It's definitely something to keep in mind, and like for Scala this positively influences the language's adoption.
Getting started?
Download and unzip the last release of Clojure. Start up a class by specifying clojure-1.3.0.jar in the classpath :java -cp clojure-1.3.0.jar clojure.main(check the version: at the time of this writing the last stable release is 1.3.0.) You can play with the Read-Eval-Print-Loop like you could do with Python or Ruby:
[15:45:20][giorgio@Desmond:~/clojure-1.3.0]$ java -cp clojure-1.3.0.jar clojure.main Clojure 1.3.0 user=> (+ 2 3) 5 user=>However, add this to your .bashrc for convenience:
alias clj="java -jar ~/clojure-1.3.0/clojure-1.3.0.jar"Now you can type clj file.clj to execute Clojure code.
An introduction to the language?
I already knew a bit of LISP, so I skipped the introductions. There is a long tutorial that helps you all the way from the imperative style, if you have not already encountered a LISP dialect like Scheme or Common LISP. There are also some fill-in examples, on the style of Scala and Ruby koans, but without the zen comments. They run in the browser, so may feel innatural.
Thus how can Clojure be explored by skipping the boring introductions? With a simple kata, a self-contained programming exercise: I prefer doing instead of reading. The FizzBuzz kata is my standard way for defining if a person is a developer, and for learning a new language or framework. It's like changing a single parameter of an experiment, such as the temperature, a weight or a concentration, to understand clearly what happens without confounding variables in the way.
However, to execute a kata I need a way to test my code. There is a testing framework shipped with Clojure, fortunately.
Testing
Testes can be defined as expressions that evaluate to true, which is all you need in a functional language.
(ns fizzbuzztests (:use clojure.test)) (deftest test-adder (is (= 24 (+ 2 22)))) (deftest test-adder (is (= 24 (+ 2 23)))) (run-tests 'fizzbuzztests)
Executing this code will show a failed test, of course:
Testing fizzbuzztests FAIL in (test-adder) (fizzbuzztest.clojure:6) expected: (= 24 (+ 2 23)) actual: (not (= 24 25)) Ran 1 tests containing 1 assertions. 1 failures, 0 errors.
The FizzBuzz kata
I executed the kata adding one test at the time, and googling a lot between a failing test (easy to write) and its implementation and subsequent refactoring. I had to look for how to define a function, where the integer module operator was, the let expression for refactoring, concatenation of strings... But I think I learned quite a bit of basic features.
The goal I set was writing a (fizzbuzz x) function. Of course the code I end up with is horrible and full of ifs (they are bad in functional programming too, right?) But it works, it's expressive and it's a starting point for learning more about Clojure.
Spoiler alert
Here's the final result, compared to an imperative language. Keep in mind that I always used an imperative language, like Java, PHP or JavaScript, so this kata may be influenced by my previous experiences (and I just discovered the defn shorthand.)
By the way, if you think JavaScript is very different from Java, this is the moment to try Clojure or other LISPs. Here's the JavaScript version:
function FizzBuzz(correspondences) { this.correspondences = correspondences; this.accept = function (number) { var result = ''; for (var divisor in this.correspondences) { if (number % divisor == 0) { result = result + this.correspondences[divisor]; } } if (result) { return result; } else { return number; } } }
And here's the Clojure one, complete with tests:
(ns fizzbuzz (:use clojure.test)) (def divides (fn [x divisor] (= 0 (mod x divisor)))) (def nameNumbers (fn [x mappingsOfNumbers] (if (empty? mappingsOfNumbers) "" (let [divisor (key (first mappingsOfNumbers)) value (val (first mappingsOfNumbers))] (if (divides x divisor) (str value (nameNumbers x (rest mappingsOfNumbers))) (nameNumbers x (rest mappingsOfNumbers))))))) (def fizzbuzz (fn [x] (let [names (nameNumbers x {3 "Fizz" 5 "Buzz"})] (if (= "" names) x names)))) (deftest test1ShouldBeLeftUntouched (is (= 1 (fizzbuzz 1)))) (deftest test2ShouldBeLeftUntouched (is (= 2 (fizzbuzz 2)))) (deftest test3isFizz (is (= "Fizz" (fizzbuzz 3)))) (deftest test5isBuzz (is (= "Buzz" (fizzbuzz 5)))) (deftest test6isFizz (is (= "Fizz" (fizzbuzz 6)))) (deftest test10isBuzz (is (= "Buzz" (fizzbuzz 10)))) (deftest test15isFizzBuzz (is (= "FizzBuzz" (fizzbuzz 15)))) (run-tests)
Opinions expressed by DZone contributors are their own.
Comments