Functional FizzBuzz in Scala With Streams and Higher Order Functions

I'm currently learning functional programming with scala and as a practice I've implemented FizzBuzz. To be honest, I made three different implementations of FizzBuzz but only the third one is good.

Stream.from(1) # Create infinite lazy stream from 1
  .map { (_, "") } # convert it to (number, word) tuple
  .map { x => if (x._1 % 3 == 0) (x._1, x._2 + "Fizz") else x } # Add "Fizz" to each 3rd word
  .map { x => if (x._1 % 5 == 0) (x._1, x._2 + "Buzz") else x } # Add "Buzz" to each 5th word
  .map { x => if (x._2 != "") x._2 else x._1.toString } # Take word or number
  .take(30) # Limit stream length
  .foreach(println) # Run everything and print results

It's even much better than usual imperative implementation and here's why.

Because composition. It's implemented as combination of small functions. This makes it easier to combine logic in different ways. Want to add one more word to be shown on every 7th step? Easy, just copy one line. Want to make it configurable? A bit harder, but still obvious how to do it.

Because proper logic grouping. Business logic is separated from interface work. First we make an infinite lazy steam with all the fizzbuzzing logic hidden inside. Then we limit it. Then we run everything and print results. That's very different from classic imperative approach where we are doing everything in a single loop. How do you cover the logic of imperative FizzBuzz with init tests? Probably, by hiding println inside dependency injected class. How do you cover functional FizzBuzz? Make a test for everything except last line. And there's no memory overhead because you never store whole stream in memory and only first element is calculated before you run foreach.

Functional programming teaches you to wrap all the logic into single pure function and decide what to do with it later. Isn't it cool?

Tags: , , ,