FizzBuzz in Clojure
This past Christmas break I had the chance to finally pick up The Joy of Clojure book and delve into the world of Lisp. Along with the common-place Merge-sort algorithm I find it beneficial to explore a new language and its capabilities by solving the FizzBizz code kata. In this post I will be explaining a couple of the implementations that I created.
Each of the four solutions below share the following predicate functions.
And can return the desired results by mapping it over a supplied range.
This solution is the most trivial taking advantage of ‘cond’ over multiple ‘if’ statements. So as to remove some boilerplate the anonymous function short-hand has been used.
## Solution 2
The solution below uses the ‘let’ statement in combination with ‘str’ to produce an output string. If the output string is empty however, the provided number is returned instead.
Conceptionaly similar to the ‘cond’ implementation - ‘some-fn’ allows us to create a function which includes a list of predicates along with their desired return values. In the case of a match not being found the supplied number is returned instead.
The final solution uses a couple of great Clojure features, including multi-arity overloading to supply a default lookup map, as well as an array map which maintains insertion order. Each entry in the map is deconstructed into its predicate and return word (key, value) and then used to test against the supplied number. If the predicate passes, the entry is kept and finally joined together to return the output using ‘apply’ and ‘str’ functions.