LoginSignup
1
1

More than 5 years have passed since last update.

Scala - 自身のインスタンスでメソッドチェインしている時の畳み込み

Last updated at Posted at 2014-03-28

環境

  • Mac OSX Version 10.9.2
  • sbt 0.13.1
  • scala 2.10.3

実装

  • specs2のソースコードから一部抜粋
case class Chain(list: List[Int]) {
  def withNewElement(e: Int): Chain = this.copy(list = (list.to[ListBuffer] += e).toList)
}
def fold(chain: Chain, list: List[Int]): Chain = {
  list match {
    case Nil => chain
    case x :: xs => fold(chain.withNewElement(x), xs)
  }
}
val chain = Chain(List(1, 2, 3))
val newChain = fold(chain, List(4, 5, 6))
def e1 = newChain must_== Chain(List(1, 2, 3, 4, 5, 6))

1
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
1
1