-
-
Save tos-kamiya/b841930ac9b1b6d8aa203aec5eee8571 to your computer and use it in GitHub Desktop.
ScalaのStreamで書いた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
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