Skip to content

Instantly share code, notes, and snippets.

@Raffaello
Last active February 26, 2018 00:15
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 Raffaello/9c8eee8000b24a558d8d3bc994831b25 to your computer and use it in GitHub Desktop.
Save Raffaello/9c8eee8000b24a558d8d3bc994831b25 to your computer and use it in GitHub Desktop.
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