Foldable.Mapped

Henning Thielemann lemming at henning-thielemann.de
Sun Jan 3 21:11:24 UTC 2016


I found the following data type useful when working with the Foldable 
class:


{- |
It holds:

> foldMap f . Mapped g = foldMap f . fmap g

but use of 'Mapped' avoids 'Functor' constraint.
-}
data Mapped f a b = Mapped (a -> b) (f a)


instance (Foldable f) => Foldable (Mapped f a) where
    foldMap g (Mapped f xs) = Fold.foldMap (g . f) xs
    foldr g x (Mapped f xs) = Fold.foldr (g . f) x xs
    foldl g x (Mapped f xs) = Fold.foldl (\acc -> g acc . f) x xs


Should it be added to Data.Foldable?


More information about the Libraries mailing list