Last active
August 29, 2015 14:12
-
-
Save marc0der/7e127d8319fd10b899e1 to your computer and use it in GitHub Desktop.
A neat little solution for FizzBuzz in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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