Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Forked from skht777/FizzBuzz.scala
Last active January 9, 2017 02:52
Show Gist options
  • Save tos-kamiya/b841930ac9b1b6d8aa203aec5eee8571 to your computer and use it in GitHub Desktop.
Save tos-kamiya/b841930ac9b1b6d8aa203aec5eee8571 to your computer and use it in GitHub Desktop.
ScalaのStreamで書いたFizzBuzz
def fizzbuzz(table: Seq[(Int, String)])(i: Int): String = {
val s = table.map { case (num, mes) =>
if (i % num == 0) mes else ""
}.mkString("")
(if (s.isEmpty) i.toString else s)
}
def fizzbuzzStream(table: Seq[(Int, String)]): Stream[String] =
Stream.from(1).map(fizzbuzz(table)(_))
object FizzBuzz extends App {
val t = Seq((3, "Fizz"), (5, "Buzz"))
println(fizzbuzzStream(t).take(100).mkString(" "))
// val fb = fizzbuzz(Seq((3, "Fizz"), (5, "Buzz")))_
// println((1 to 10).map(fb).mkString(" "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment