2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

逆Fizzbuzzを少し見やすく。 #nascala

Posted at

第9回なごやかscalaで話題になった、逆FizzbuzzのScalaコードを自分なりに見やすくした。根本的なアルゴリズムは変えていない。

InverseFizzbuzz.scala
  def fizzbuzzes(range : Seq[Int]) =
    range.map(x => (x%3, x%5)).collect {
      case (0, 0) => "fizzbuzz"
      case (0, _) => "fizz"
      case (_, 0) => "buzz"
    }

  val all = for {
    a <- (1 to 100)
    b <- (a to 100)
  } yield (a to b)

  val io = all.map (input => (input, fizzbuzzes(input)))

  def answer(expected : Seq[String]) = io.
    filter { case (i, o) => o == expected }.
    minBy { case (i, _) => i.length }.
    _1

参考:

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?