deepseq icon indicating copy to clipboard operation
deepseq copied to clipboard

Offer Foldable helper

Open treeowl opened this issue 9 years ago • 1 comments

It's possible to define a reasonable NFData instance for any Foldable type. We can offer a suitable rnf to help.

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` ()

treeowl avatar Jul 14 '16 03:07 treeowl

As I mentioned on the PR, this often isn't reasonable at all, since there could be fields not covered by the Foldable instance, such as any field that just has a fixed type like String.

rwbarton avatar Jul 27 '16 21:07 rwbarton