Why are `sum` and `product` defined via foldMap' and not foldl'?
Tom Ellis
tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Wed Dec 23 09:58:41 UTC 2020
On Wed, Dec 23, 2020 at 01:32:38AM -0500, Viktor Dukhovni wrote:
> Is there a compelling reason for:
>
> sum = getSum #. foldMap' Sum
> product = getProduct #. foldMap' Product
>
> rather than:
>
> sum = foldl' (+) 0
> product = foldl' (*) 1
[...]
> The `foldl'` variant looks substantially more efficient (at least for
> lists), is there some important context in which `foldMap'` is
> preferable?
Have you benchmarked with optimisations on? I would not be surprised
if a small amount of inlining brings them to the same speed.
Tom
More information about the Libraries
mailing list