Proposal: add foldMapA to Data.Foldable or Control.Applicative
David Feuer
david.feuer at gmail.com
Wed May 8 04:02:49 UTC 2019
On Tue, May 7, 2019 at 11:49 PM Isaac Elliott <isaace71295 at gmail.com> wrote:
> I've previously suggested similar things, like:
>
> allA :: (Applicative f, Foldable t) => (a -> f Bool) -> t a -> f Bool
> allA f = fmap getAll . getAp . foldMap (Ap . fmap All . f)
>
> I think such functions are very convenient.
>
I think your allA is arguably better-justified than foldMapA because the
simple
implementation you demonstrate could run into trouble if fmap is expensive.
Better:
allA :: (Applicative f, Foldable t) => (a -> f Bool) -> t a -> f Bool
allA f = getFall . foldMap (Fall . f)
newtype Fall f = Fall {getFall :: f Bool}
instance Applicative f => Semigroup (Fall f) where
Fall x <> Fall y = Fall $ liftA2 (&&) x y
instance Applicative f => Monoid (Fall f) where
mempty = Fall (pure True)
I keep wondering if there's some nice way (short of Coyoneda or similar) to
generalize this
sort of thing. I haven't thought of one yet.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190508/0dc8c6fe/attachment.html>
More information about the Libraries
mailing list