Last active
February 26, 2018 00:15
-
-
Save Raffaello/9c8eee8000b24a558d8d3bc994831b25 to your computer and use it in GitHub Desktop.
FizzBuzz
This file contains hidden or 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
(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") |
This file contains hidden or 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
(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") |
This file contains hidden or 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
(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") |
This file contains hidden or 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
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