<div dir="ltr">I don't see any reason to not do this. Someone just needs to put a differential on phabricator, adding foldMap, a default implementation, and documentation of it. It would be maybe 6 lines total. The default implementation of foldMap' would use foldl' and that would be optimal for everything in base.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 28, 2018 at 6:38 PM, Simon Jakobi via Libraries <span dir="ltr"><<a href="mailto:libraries@haskell.org" target="_blank">libraries@haskell.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Is there any chance that foldMap' might still make it into base-4.12?<br>
<div class="HOEnZb"><div class="h5">Am Sa., 9. Juni 2018 um 10:23 Uhr schrieb Edward Kmett <<a href="mailto:ekmett@gmail.com">ekmett@gmail.com</a>>:<br>
><br>
> +1 from me.<br>
><br>
> -Edward<br>
><br>
> On Jun 8, 2018, at 9:11 PM, <a href="mailto:evan@evan-borden.com">evan@evan-borden.com</a> <<a href="mailto:evan@evanrutledgeborden.dreamhosters.com">evan@evanrutledgeborden.<wbr>dreamhosters.com</a>> wrote:<br>
><br>
> +1 We utilize a foldMap' in the freckle codebase.<br>
><br>
> On Fri, Jun 8, 2018 at 1:10 PM, Daniel Cartwright <<a href="mailto:chessai1996@gmail.com">chessai1996@gmail.com</a>> wrote:<br>
>><br>
>> +1<br>
>><br>
>> On Fri, Jun 8, 2018 at 9:20 AM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com">andrew.thaddeus@gmail.com</a>> wrote:<br>
>>><br>
>>> I propose adding another method to the Foldable typeclass: foldMap'<br>
>>><br>
>>> This has the same behavior as foldMap except that it is strict in the accumulator. This can lead to considerable performance gains when the user knows that monoidal append is strict both arguments. Consider the following example (available as a gist at <a href="https://gist.github.com/andrewthad/f79b7022725532baf709514cf08c3955" rel="noreferrer" target="_blank">https://gist.github.com/<wbr>andrewthad/<wbr>f79b7022725532baf709514cf08c39<wbr>55</a>):<br>
>>><br>
>>>   {-# LANGUAGE BangPatterns #-}<br>
>>>   {-# OPTIONS_GHC -O2 #-}<br>
>>>   import Gauge<br>
>>>   import Data.Foldable<br>
>>>   import qualified Data.Set as S<br>
>>><br>
>>>   foldMap' :: (Monoid m, Foldable f) => (a -> m) -> f a -> m<br>
>>>   foldMap' f = foldl' (\ !acc a -> acc <> f a) mempty<br>
>>><br>
>>>   numbers :: [Int]<br>
>>>   numbers = [1..4000]<br>
>>><br>
>>>   intToSet :: Int -> S.Set Int<br>
>>>   intToSet i = S.singleton (mod i 10)<br>
>>><br>
>>>   main :: IO ()<br>
>>>   main = defaultMain<br>
>>>     [ bench "lazy" $ whnf (foldMap intToSet) numbers<br>
>>>     , bench "strict" $ whnf (foldMap' intToSet) numbers<br>
>>>     ]<br>
>>><br>
>>> Here are the results we get from running this:<br>
>>><br>
>>>   benchmarked lazy<br>
>>>   time                 178.8 μs   (176.1 μs .. 183.1 μs)<br>
>>>                        0.996 R²   (0.993 R² .. 0.998 R²)<br>
>>>   mean                 180.8 μs   (179.1 μs .. 183.3 μs)<br>
>>>   std dev              7.242 μs   (5.856 μs .. 9.304 μs)<br>
>>>   variance introduced by outliers: 20% (moderately inflated)<br>
>>><br>
>>>   benchmarked strict<br>
>>>   time                 108.4 μs   (106.1 μs .. 111.0 μs)<br>
>>>                        0.997 R²   (0.996 R² .. 0.999 R²)<br>
>>>   mean                 107.9 μs   (107.0 μs .. 109.3 μs)<br>
>>>   std dev              3.672 μs   (2.451 μs .. 6.220 μs)<br>
>>>   variance introduced by outliers: 15% (moderately inflated)<br>
>>><br>
>>> These performance gains are considerable. It needs to be a method of Foldable and not just a function written using foldl' for the same reason that the lazy foldMap needs to be a method of Foldable. There are types for which the default implementation can be improved upon. This is a non-breaking change since there is a sensible default implementation.<br>
>>><br>
>>> --<br>
>>> -Andrew Thaddeus Martin<br>
>>> ______________________________<wbr>_________________<br>
>>> Libraries mailing list<br>
>>> <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
>>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/libraries</a><br>
>><br>
>><br>
>> ______________________________<wbr>_________________<br>
>> Libraries mailing list<br>
>> <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/libraries</a><br>
>><br>
><br>
><br>
><br>
> --<br>
> --<br>
> Evan Borden<br>
><br>
> ______________________________<wbr>_________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/libraries</a><br>
><br>
> ______________________________<wbr>_________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/libraries</a><br>
______________________________<wbr>_________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/libraries</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">-Andrew Thaddeus Martin</div>
</div>