Why are `sum` and `product` defined via foldMap' and not foldl'?
Oleg Grenrus
oleg.grenrus at iki.fi
Wed Dec 23 10:44:06 UTC 2020
Because the related change proposal(s) are never accepted nor implemented.
Most recent one is quite recent though.
https://mail.haskell.org/pipermail/libraries/2020-October/030862.html
- Oleg
On 23.12.2020 8.32, 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
>
> A quick ghci session with today's GHC head yields:
>
> λ> import qualified Data.Foldable as F
> λ> :set +s
>
> λ> F.sum [0..10000000]
> 50000005000000
> (2.98 secs, 1,612,368,368 bytes)
>
> λ> F.foldl' (+) 0 [0..10000000]
> 50000005000000
> (0.28 secs, 880,065,112 bytes)
>
> The `foldl'` variant looks substantially more efficient (at least for
> lists), is there some important context in which `foldMap'` is
> preferable?
>
More information about the Libraries
mailing list