haskell / deepseq

Deep evaluation of data structures
http://hackage.haskell.org/package/deepseq
Other
41 stars 29 forks source link

Offer Foldable helper #17

Open treeowl opened 8 years ago

treeowl commented 8 years ago

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` ()
rwbarton commented 8 years ago

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.