我只看了List.scala的实现foldRight().
override def reverse: List[A] = {
var result: List[A] = Nil
var these = this
while (!these.isEmpty) {
result = these.head :: result
these = these.tail
}
result
}
override def foldRight[B](z: B)(op: (A, B) => B): B =
reverse.foldLeft(z)((right, left) => op(left, right))
Run Code Online (Sandbox Code Playgroud)
据我了解,呼吁foldRight对List结果调用theList.reverse.foldLeft(...).
是List.foldRight与实施foldLeft以便利用一个单一的堆栈帧,而不是使用多个堆栈帧与foldLeft?
scala ×1