[英]Scala fold with a function and initial value
给定一个泛型函数,你如何实现折叠函数?
def dofold[A, B](list: List[A], func: (B, A) => B, initial: B): B
你可以看看它是如何在 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.