4

The most elegant implementation of FizzBuzz

 3 years ago
source link: https://blog.klipse.tech/clojure/2020/09/11/fizbuzz-clj.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The most elegant implementation of FizzBuzz

Sep 11, 2020 • Yehonathan Sharvit

What is FizzBuzz?

The FizzBuzz test is an interview question designed to help filter out the 99.5% of programming job candidates who can’t seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”

There are a lot of ways to write the FizzBuzz program. See for instance the one using Clojure pattern matching. Today, I’d like to share with you the most elegant implementation of FizzBuzz I’ve ever seen. It’s elegant because it doesn’t make use of any imperative constructs (like if statements), but only functional programming constructs (like infinite lazy sequences and function composition).

Pure

This implementation was developed by Dierk Konig and Kevlin Henney presented a Haskell version of it - in his amazing talk Declarative Thinking, Declarative Practice.

In this article, we are going to present an interactive version of Kevlin’s code in Clojure.

The elegant code

Take a look at this marvel: no if statements - only 3 infinite lazy sequences and function composition.

This is exactly what we call - Purely Functional:

xxxxxxxxxx
;loaded from gist: https://gist.github.com/viebel/b133efde8669d6c0630ee6895c1797c6
(defn choice [a b]
  (clojure.string/join (or (seq a) (seq b))))
(defn fizzbuzz [n]
  (let [fizzes (cycle ["" "" "Fizz"])
        buzzes (cycle ["" "" "" "" "Buzz"])
        words (map str fizzes buzzes)
        numbers (map str (rest (range)))]
    (take n (map max words numbers))))
the evaluation will appear here (soon)...

And it works like a charm:

xxxxxxxxxx
(fizzbuzz 19)
xxxxxxxxxx
the evaluation will appear here (soon)...

Feel free to modify 19 - the code snippets are live and interactive powered by the Klipse plugin:

  1. Live: The code is executed in your browser
  2. Interactive: You can modify the code and it is evaluated as you type

If you are a bit skeptic (yet) about the elegance of this implementation, you might want to read Dierk Konig’s article: he explains in details the pragmatic advantages of his code.

Further details

You probably wonder what is this choice function and why does max do when it receives 2 strings?

Well, ClojureScript runs on top of JavaScript - and in JavaScript, strings are comparable:

xxxxxxxxxx
(> "Fizz" "")
xxxxxxxxxx
the evaluation will appear here (soon)...
xxxxxxxxxx
(max "Fizz" "")
xxxxxxxxxx
the evaluation will appear here (soon)...

But that is an ugly trick - that doesn’t work in Clojure. So it’s much better to use the choice function - that returns the first non-empty string of the two it receives:

xxxxxxxxxx
(choice "abc" "")
xxxxxxxxxx
the evaluation will appear here (soon)...

And it works also:

(fizzbuzz-clean 15)
If you enjoy this kind of interactive articles would you consider a (small) donation💸 on Patreon or at least giving a star⭐ for the Klispe repo on Github?

to stay up-to-date with the coolest interactive articles around the world.

Discover more cool interactive articles about javascript, clojure[script], python, ruby, scheme, c++ and even brainfuck!

Give Klipse a Github star to express how much you appreciate Code Interactivity.

Subscribe to the Klipse newsletter:

Feel free to email me [email protected] for getting practical tips and tricks in writing your first interactive blog post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK