Get the latest tech news

FizzBuzz Through Monoids


Some decade ago I read a good implementation of fizzbuzz. What set it apart was its excellent modularity.

Whatever we get out of mconcat, we pass it to fromMaybe (show i) which replaces any Nothing values with the string representation of the number coming into the function, but passes through any actual values it receives intact. This is the full fizzbuzz function that converts a number to the correct textual representation. To make it an actual program, we loop through all numbers[1..100], convert them with fizzbuzz, and print the result.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of FizzBuzz

FizzBuzz

Photo of Monoids

Monoids

Related news:

News photo

The FizzBuzz that did not get me the job

News photo

Fold-... and Monoids

News photo

FizzBuzz in Powerchords