我已经了解了foldLeft和reducleft的基本区别
foldLeft:
必须传递初始值
reduceLeft:
以集合的第一个元素作为初始值 如果集合为空,则抛出异常
还有其他区别吗?
有什么特殊的原因要有两个功能相似的方法吗?
我已经了解了foldLeft和reducleft的基本区别
foldLeft:
必须传递初始值
reduceLeft:
以集合的第一个元素作为初始值 如果集合为空,则抛出异常
还有其他区别吗?
有什么特殊的原因要有两个功能相似的方法吗?
当前回答
为了真正理解折叠/缩减的作用, 检查这个:http://wiki.tcl.tk/17983 非常好的解释。一旦你掌握了折叠的概念, Reduce会和上面的答案一起出现: list.tail.foldLeft (list.head) (_)
其他回答
规模2.13.3,演示:
val names = List("Foo", "Bar")
println("ReduceLeft: "+ names.reduceLeft(_+_))
println("ReduceRight: "+ names.reduceRight(_+_))
println("Fold: "+ names.fold("Other")(_+_))
println("FoldLeft: "+ names.foldLeft("Other")(_+_))
println("FoldRight: "+ names.foldRight("Other")(_+_))
输出:
ReduceLeft: FooBar
ReduceRight: FooBar
Fold: OtherFooBar
FoldLeft: OtherFooBar
FoldRight: FooBarOther
《Scala函数式编程原则》(Martin Odersky):
函数reduceLeft是根据一个更通用的函数foldLeft定义的。 foldLeft类似于reduceLeft,但接受累加器z作为附加参数,当在空列表上调用foldLeft时返回: (List (x1,…, xn) foldLeft z)(op) =(…(z op x1) op…)op x
[与reducleft相反,它会在空列表上调用时抛出异常。]
本课程(见第5.5讲)提供了这些函数的抽象定义,说明了它们的区别,尽管它们在模式匹配和递归的使用上非常相似。
abstract class List[T] { ...
def reduceLeft(op: (T,T)=>T) : T = this match{
case Nil => throw new Error("Nil.reduceLeft")
case x :: xs => (xs foldLeft x)(op)
}
def foldLeft[U](z: U)(op: (U,T)=>U): U = this match{
case Nil => z
case x :: xs => (xs foldLeft op(z, x))(op)
}
}
注意,foldLeft返回一个U类型的值,它不一定与List[T]的类型相同,但reducelefleft返回一个与List相同类型的值)。
为了真正理解折叠/缩减的作用, 检查这个:http://wiki.tcl.tk/17983 非常好的解释。一旦你掌握了折叠的概念, Reduce会和上面的答案一起出现: list.tail.foldLeft (list.head) (_)
作为参考,如果将reduceLeft应用于空容器,将出现以下错误。
java.lang.UnsupportedOperationException: empty.reduceLeft
重写要使用的代码
myList foldLeft(List[String]()) {(a,b) => a+b}
是一个潜在的选择。另一种方法是使用reduceLeftOption变体,它返回一个Option包装的结果。
myList reduceLeftOption {(a,b) => a+b} match {
case None => // handle no result as necessary
case Some(v) => println(v)
}
它们都在Scala标准库中的基本原因可能是因为它们都在Haskell标准库中(称为foldl和foldl1)。如果reducleft不是,那么在不同的项目中,它通常被定义为一个方便的方法。