Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

環境

  • 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))

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away