I wanted to create a FizzBuzz implementation that both showcases the strengths of Scala (Functional paradigm and collections) and is general enough to anticipate the typical interview follow-up questions. i.e. What if fizz was for factors of 4 instead? What if we wanted to replace factors of 7 with "foo"?
object FizzBuzz extends App {
def fizzbuzz(factors: Map[Int, String])(currentNumber: Int): String = {
val words = factors.filter(currentNumber % _._1 == 0).values
if (words.isEmpty) currentNumber.toString else words.mkString("")
}
private final val Factors = Map(3 -> "fizz", 5 -> "buzz")
private final val Range = 1 to 100
Range.map(fizzbuzz(Factors)).foreach(println)
}
I would appreciate any feedback on how I can make my Scala more idiomatic.