Skip to content

Instantly share code, notes, and snippets.

@Raffaello
Last active Feb 26, 2018
Embed
What would you like to do?
FizzBuzz
(1 to 100).map { x =>
val str = StringBuilder.newBuilder
if(x % 3 == 0) str append "Fizz"
if(x % 5 == 0) str append "Buzz"
if(str.size > 0) str.toString else x.toString
}.mkString("\n")
(1 to 100).map {
case x if (x % 15 == 0) => "FizzBuzz"
case x if (x % 5 == 0) => "Buzz"
case x if (x % 3 == 0) => "Fizz"
case x => x.toString
}.mkString("\n")
(1 to 100).map { x =>
(x % 5, x % 3) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Buzz"
case (_, 0) => "Fizz"
case _ => x.toString
}
}.mkString("\n")
def fizzBuzz(i: Int): String =
Seq(15 -> "FizzBuzz", 5 -> "Buzz", 3 -> "Fizz")
.find(i % _._1 == 0)
.map(_._2)
.getOrElse(i.toString)
(1 to 100).map(fizzBuzz).mkString("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment