General FizzBuzz in Scala 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 -&gt; "fizz", 5 -&gt; "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.
Map is not guaranteed to impose or preserve any order for its entries.  For example, if I change Factors to …

private final val Factors = Map(3 -&gt; "a", 5 -&gt; "b", 6 -&gt; "c", 7 -&gt; "d", 8 -&gt; "e", 9 -&gt; "f")


… I get:


⁞
89
bcfa
d
92
a
94
b
cae
97
d
fa
b



So, the fact that you get "fizzbuzz" rather than "buzzfizz" is based on luck, not skill.  If you want to guarantee "fizzbuzz", you need a scala.collection.immutable.SortedMap.