在给定通用函数的情况下,如何实现折叠函数?
def dofold[A, B](list: List[A], func: (B, A) => B, initial: B): B
答案 0 :(得分:1)
您可以查看它如何在Scala标准库或Haskell中实现
https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/immutable/List.scala#L407 https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/LinearSeqOptimized.scala#L122 https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/LinearSeqOptimized.scala#L133
https://wiki.haskell.org/Foldr_Foldl_Foldl%27(也许此链接是最好的)
http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Foldable.html#foldr http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Foldable.html#foldl