Sunday, June 8, 2014

Practising Clojure sequences using FizzBuzz and REPL Driven Development

I recently blogged about my version of the FizzBuzz kata in Clojure using Midje comparing it with a version I did some time ago in Racket's Advanced Student Language.

Even though both solutions were very similar, doing the kata this time in Clojure was useful to practise with Midje.

To try to get to a different solution for the kata and use the Clojure sequence library, I set myself a constraint:
Instead of mapping just a function on the sequence of numbers, I had to find several transformations that could be mapped one after another on sequences to get the same result.

Since I had to do a lot of trial and error, I prefered to use REPL Driven Development this time.

This was the REPL session to get to the first alternative version (eliminating a lot of errors..):

From this REPL session I got this alternative version for fizz-buzz:

It's more complicated that the original version but it served me to practise some interesting Clojure features: recur, partial, map and let.

Then I tried a different approach:
Reducing the sequence of transformation functions so that I can map the resulting transformation function on the sequence of numbers.

Again I worked in the REPL first:

From this second REPL session I got this other alternative version for fizz-buzz:

which is again more complicated than the original version but served me to work with other intersting Clojure's features: reduce, comp and reverse.

After these two versions of FizzBuzz I did a web search to find nicer ways to use the Clojure's sequence library to implement FizzBuzz and I found these two in Rosseta Code that I specially like (I modified them a bit so they return the same that the precious versions):

These two last versions will serve me to learn about if-let and cycle.

Ok, that was all I got today.

Just to sum up, I think there is a lot you can get from even a simple kata like FizzBuzz if you set yourself some constraints and try different variations.


PS: You'll find other variations of FizzBuzz kata in these previous posts: this one and this other one.

No comments:

Post a Comment