Skip to content

Instantly share code, notes, and snippets.

@nesheep5
Created April 11, 2016 22:32
Show Gist options
  • Select an option

  • Save nesheep5/d0a592427a68c0d99a7a301b0d441cf3 to your computer and use it in GitHub Desktop.

Select an option

Save nesheep5/d0a592427a68c0d99a7a301b0d441cf3 to your computer and use it in GitHub Desktop.
FizzBuzz.scala
// パターン1
for(x <- 1 to 100) x match {
case x if x % 3 == 0 && x % 5 == 0 => println("FizzBuzz")
case x if x % 3 == 0 => println("Fizz")
case x if x % 5 == 0 => println("Buzz")
case _ => println(x)
}
// パターン2
for(x <- 1 to 100) (x % 3, x % 5) match {
case (0,0) => println("FizzBuzz")
case (0,_) => println("Fizz")
case (_,0) => println("Buzz")
case _ => println(x)
}
// パターン3
def fizzbuzz(x:Int )= {
(x%3,x%5) match {
case (0,0) => "FizzBuzz"
case (0,_) => "Fizz"
case (_,0) => "Buzz"
case _ => x
}
}
(1 to 100).map(fizzbuzz).foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment