Skip to content

Instantly share code, notes, and snippets.

@marc0der
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marc0der/7e127d8319fd10b899e1 to your computer and use it in GitHub Desktop.
Save marc0der/7e127d8319fd10b899e1 to your computer and use it in GitHub Desktop.
A neat little solution for FizzBuzz in Scala
object FizzBuzzer {
def process(i: Int) = i match {
case a if contains("3")(a) => "lucky"
case a if multiple(15)(a) => "fizzbuzz"
case a if multiple(5)(a) => "buzz"
case a if multiple(3)(a) => "fizz"
case _ => i.toString
}
def contains(s: String)(a: Int) = a.toString.contains(s)
def multiple(m: Int)(i: Int) = i % m == 0
def doIt(r: Range): String = r.map(process).mkString(" ")
println(doIt(1 to 20))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment