Clojure Kata #1 – Fizz Buzz
December 19, 2013I’ve been learning more about the ins and outs of Clojure lately, so I decided to practice a couple of katas. The obvious first one is the Fizz Buzz kata.
Here’s the code of my first take:
(defn fizz? [number]
(zero? (rem number 3)))
(defn buzz? [number]
(zero? (rem number 5)))
(defn fizz-buzz? [number]
(and (fizz? number) (buzz? number)))
(defn fizz-buzz [number]
(if (fizz-buzz? number) "fizzbuzz"
(if (fizz? number) "fizz"
(if (buzz? number) "buzz" number))))
(defn run-fizz-buzz []
(map fizz-buzz (range 1 101)))
For this first attempt, I set out the goal to come up with some very small functions to accomplish the task at hand. This kind of worked out fine except for the fizz-buzz function, which doesn’t feel very idiomatic to me probably caused by the nested if statements.
So for the second attempt, I tried to get rid of these if statements, and this was the result:
(defn fizz? [number]
(zero? (rem number 3)))
(defn buzz? [number]
(zero? (rem number 5)))
(defn fizz-buzz? [number]
(and (fizz? number) (buzz? number)))
(defn fizz-buzz [number]
(cond
(fizz-buzz? number) "fizzbuzz"
(fizz? number) "fizz"
(buzz? number) "buzz"
:else number))
(defn run-fizz-buzz []
(map fizz-buzz (range 1 101)))
Using the cond macro I was able to remove the nested if statements, and it also slightly reduced the number of parenthesis. So far, so good.
But maybe having so many functions is overkill. So for the next take I wanted to get rid of the fizz?, buzz? and fizz-buzz? functions and move the meat into the fizz-buzz function itself without giving up too much on readability. Here’s the code of my third attempt:
(defn fizz-buzz [number]
(let [fizz? (zero? (rem number 3))
buzz? (zero? (rem number 5))
fizz-buzz? (and fizz? buzz?)]
(cond
fizz-buzz? "fizzbuzz"
fizz? "fizz"
buzz? "buzz"
:else number)))
(defn run-fizz-buzz []
(map fizz-buzz (range 1 101)))
Here I used the let special form in order to define the fizz?, buzz? and fizz-buzz? lexical parameter bindings that can then be used inside the expression of the function body. Notice the conciseness of this last attempt compared to the first.
There’s probably someone that could write a single-line function in Clojure that does exactly the same thing, but that’s not the point. Code katas are fun!
Until next time.
If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at info. @ principal-it .be
Jan Van Ryswyck
Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.
Comments
Writing Maintainable
Unit Tests
Watch The Videos
Latest articles
-
Contract Tests - Parameterised Test Cases
June 28, 2023
-
Contract Tests - Abstract Test Cases
April 12, 2023
-
Contract Tests
February 1, 2023
-
The Testing Quadrant
June 15, 2022
-
Tales Of TDD: The Big Refactoring
February 2, 2022
Tags
- .NET
- ALT.NET
- ASP.NET
- Agile
- Announcement
- Architecture
- Behavior-Driven Development
- C++
- CQRS
- Clojure
- CoffeeScript
- Community
- Concurrent Programming
- Conferences
- Continuous Integration
- Core Skills
- CouchDB
- Database
- Design Patterns
- Domain-Driven Design
- Event Sourcing
- F#
- Fluent Interfaces
- Functional Programming
- Hacking
- Humor
- Java
- JavaScript
- Linux
- Microsoft
- NHibernate
- NoSQL
- Node.js
- Object-Relational Mapping
- Open Source
- Reading
- Ruby
- Software Design
- SourceControl
- Test-Driven Development
- Testing
- Tools
- Visual Studio
- Web
- Windows
Disclaimer
The opinions expressed on this blog are my own personal opinions. These do NOT represent anyone else’s view on the world in any way whatsoever.
About
Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.
Latest articles
Contract Tests - Parameterised Test Cases
Contract Tests - Abstract Test Cases
Contract Tests
The Testing Quadrant
Contact information
(+32) 496 38 00 82
info @ principal-it .be