Skip to content

Instantly share code, notes, and snippets.

@P7h
Created February 4, 2017 15:49
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 P7h/406eba65b82bc20bcd330ff25426f348 to your computer and use it in GitHub Desktop.
Save P7h/406eba65b82bc20bcd330ff25426f348 to your computer and use it in GitHub Desktop.
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