Re:It never makes sense to use foldl on lists in Haskell (2019)
by skrebbel on Saturday March 21, 2020 @03:32AM
(#22645179)
> Interestingly, JavaScript also has `Array.prototype.reduceRight`, but it's not equivalent to a `foldr`. It processes the list from right-to-left, so is approximately equivalent to `reverse` followed by `foldl'`.
I don't understand how that's different in practice. Can you elaborate?
Re:It never makes sense to use foldl on lists in Haskell (2019)
by pansa2 on Saturday March 21, 2020 @04:31AM
(#22645330)
Although `foldr` accumulates the result right-to-left, it processes the list left-to-right, which allows it to short-circuit.
`reduceRight` processes and accumulates right-to-left, so it doesn’t have the short-circuiting ability of `foldr`.
The standard libraries of strict languages agree. Python's `functools.reduce` and JavaScript's `Array.prototype.reduce` perform strict left folds equivalent to Haskell's `foldl'`.
Interestingly, JavaScript also has `Array.prototype.reduceRight`, but it's not equivalent to a `foldr`. It processes the list from right-to-left, so is approximately equivalent to `reverse` followed by `foldl'`.