Skip to content

Instantly share code, notes, and snippets.

@michalrus
Created November 22, 2014 22:23
Show Gist options
  • Save michalrus/05efe311f815f96e6a4a to your computer and use it in GitHub Desktop.
Save michalrus/05efe311f815f96e6a4a to your computer and use it in GitHub Desktop.
object Main extends App {
def fizzBuzz(num: Int, mapping: Map[Int, String]): List[Either[Int, String]] =
(1 to num).toList map { i ⇒
mapping.toList.sorted flatMap {
case (k, v) if i % k == 0 ⇒ List(v)
case _ ⇒ Nil
} match {
case Nil ⇒ Left(i)
case rs ⇒ Right(rs.mkString)
}
}
fizzBuzz(16, Map(3 → "Fizz", 5 → "Buzz")) foreach println
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment