Skip to content

Instantly share code, notes, and snippets.

@P7h P7h/FizzBuzz.scala
Created Feb 4, 2017

Embed
What would you like to do?
FizzBuzz in Scala
object FizzBuzz {
def main(args: Array[String]) {
(1 to 30) map fizzbuzz map println
}
case class MultipleOf(n: Int) {
def unapply(x: Int): Option[Int] = {
if(x % n == 0) {
Some(x/n)
} else {
None
}
}
}
val multipleOfThree = new MultipleOf(3)
val multipleOfFive = new MultipleOf(5)
val multipleOfFifteen = new MultipleOf(15)
def fizzbuzz (x: Int) = {
x match {
case multipleOfFifteen(_) => "FizzBuzz"
case multipleOfThree(_) => "Fizz"
case multipleOfFive(_) => "Buzz"
case _ => x
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.