什么时候应该使用reduceLeft,reduceRight,foldLeft,foldRight,scanLeft或scanRight?
我想要一个直觉/概述他们的差异 - 可能有一些简单的例子.
def fibSeq(n: Int): List[Int] = {
var ret = scala.collection.mutable.ListBuffer[Int](1, 2)
while (ret(ret.length - 1) < n) {
val temp = ret(ret.length - 1) + ret(ret.length - 2)
if (temp >= n) {
return ret.toList
}
ret += temp
}
ret.toList
}
所以上面是我使用Scala生成一个Fibonacci序列的代码n.我想知道Scala中是否有更优雅的方法可以做到这一点?