Proposal: Add Foldable helper to Control.DeepSeq

Edward Kmett ekmett at gmail.com
Thu Jul 14 19:09:26 UTC 2016


The monoid may be more efficient if the Foldable is constructed to take
advantage of internal sharing.

-Edward

On Thu, Jul 14, 2016 at 11:52 AM, David Feuer <david.feuer at gmail.com> wrote:

> I guess these are equivalent to the simpler expressions
>
> rnfLTR = foldl' (const rnf) ()
> rnfRTL = foldr' (\x _ -> rnf x) ()
>
> On Jul 13, 2016 11:16 PM, "David Feuer" <david.feuer at gmail.com> wrote:
>
>> As I describe in https://github.com/haskell/deepseq/issues/17 it is
>> possible to implement an NFData instance for any Foldable type, and we
>> can offer a function to help do that:
>>
>> data Unit = Unit
>>
>> instance Monoid Unit where
>>   mempty = Unit
>>   Unit `mappend` Unit = Unit -- strict in both arguments, unlike ()
>>
>> rnfFoldable :: (Foldable f, NFData a) => f a -> ()
>> rnfFoldable xs = foldMap (\x -> rnf x `seq` Unit) xs `seq` ()
>>
>> This could be used like so:
>>
>> instance NFData a => NFData (F a) where
>>   rnf = rnfFoldable
>>
>> This version forces from left to right. It would be possible to offer
>> another version that forces from right to left:
>>
>> data Unit2 = Unit2
>>
>> instance Monoid Unit2 where
>>   mempty = Unit2
>>   x `mappend` Unit2 = x `seq` Unit2
>>
>> rnfFoldableRTL :: (Foldable f, NFData a) => f a -> ()
>> rnfFoldableRTL xs = foldMap (\x -> rnf x `seq` Unit2) xs `seq` ()
>>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160714/77bef7b4/attachment-0001.html>


More information about the Libraries mailing list