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