Scala的foldLeft和foldRight
FoldLeft
定义如下:
override /*TraversableLike*/
def foldLeft[B](z: B)(f: (B, A) => B): B = {
var acc = z
var these = this
while (!these.isEmpty) {
acc = f(acc, these.head)
these = these.tail
}
acc
}
- z the start value
- f 操作函数(累积器,these.head)