Skip to content

Instantly share code, notes, and snippets.

@mogproject
Last active December 23, 2015 04:39
Show Gist options
  • Save mogproject/6581465 to your computer and use it in GitHub Desktop.
Save mogproject/6581465 to your computer and use it in GitHub Desktop.
FizzBuzz in Scala
object FizzBuzz {
private val divs = List((3, "Fizz"), (5, "Buzz"))
def fizzBuzz(n: Int): String = {
val ret = (divs map {case (i, s) => if (n % i == 0) s else ""}).mkString
if (ret.isEmpty) n.toString else ret
}
def main(args: Array[String]) {
def printAll(n: Int) { (1 to n) foreach {n => println(fizzBuzz(n))} }
// printAll(args(0).toInt)
printAll(100)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment