FizzBuzz in Clojure
I’ve recently started working in Clojure seriously. I’ve been watching Rich Hickeys talks since almost a year now, and everytime I try to learn another language like “Go” - wise words of Rich echo in my ear: "Don’t complect".
“Simplicity is hard work. But, there’s a huge payoff. The person who has a genuinely simpler system - a system made out of genuinely simple parts, is going to be able to affect the greatest change with the least work. He’s going to kick your ass. He’s gonna spend more time simplifying things up front and in the long haul he’s gonna wipe the plate with you because he’ll have that ability to change things when you’re struggling to push elephants around.” - Rich Hickey
I’ve always heard folks say that Lisp will liberate you, it will force you to think in ways you couldn’t even imagine. This has been bugging me for years, especially since I’ve read this article by Paul Graham where he described about the “Blub Pradox":
As long as our hypothetical Blub programmer is looking down the power continuum, he knows he’s looking down. Languages less powerful than Blub are obviously less powerful, because they’re missing some feature he’s used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn’t realize he’s looking up. What he sees are merely weird languages. He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well. Blub is good enough for him, because he thinks in Blub. - Paul Graham on Beating the Averages
I finally decided to pull the trigger and get good at Clojure.
For the time being, I’m lurking the Clojurians slack channel and Clojure subreddit because what better way to learn more about a language than to directly interact with the community! (I’m also going through BraveClojure)
I saw someone asking another beginner if they could do the FizzBuzz in clojure, and I thought to myself "well that must be easy".
Never have I ever felt so disoriented on trying to solve such a simple problem. I was out of my element for a few seconds, my hands didn’t move in reflex like they’ve been moving since years. I had to pause. I had to think.
This is when I realised, Clojure will help me get a different perspective to programming. It will help me evolve. Which is exactly what I need.
So this is what I came up with after a few minutes of thinking:
; fizzbuzz
(defn checkFizz [num]
(cond
(zero? (mod num 15)) "FizzBuzz"
(zero? (mod num 5)) "Buzz"
(zero? (mod num 3)) "Fizz"
:else num))
(->>
(take 100 (iterate inc 1)) ; i like this more than (range 1 101)
(map checkFizz)
(run! println))
I then tried solving it in python (my hands moved on their own and I didn’t even have to think):
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 5 == 0:
print("Buzz")
elif i % 3 == 0:
print("Fizz")
else:
print(i)
This left me with a feeling I hadn’t been able to grasp on to while programming in the past few years. A feeling of pure joy. I know this example does not portray that, but as a beginner it was a wonderful feeling.
I think there could me many many more ways to solve FizzBuzz in clojure and I’ll definitely be exploring them in future articles. So stay tuned!
“Rich Hickey once said programmers know the benefits of everything and the tradeoffs of nothing…an approach that can lead a project down a path of frustra‐ ted developers and unhappy customers. But as an architect you must consider the tradeoffs of every new library, language, pattern, or approach and quickly make decisions often with incomplete information.”