module Data.Foldable.Ext ( foldMapA
                         , foldMapAlternative
                         ) where

import           Control.Applicative (Alternative)
import           Data.Foldable       (asum, fold)

foldMapAlternative :: (Traversable t, Alternative f) => (a -> f b) -> t a -> f b
foldMapAlternative :: (a -> f b) -> t a -> f b
foldMapAlternative a -> f b
f t a
xs = t (f b) -> f b
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum (a -> f b
f (a -> f b) -> t a -> t (f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t a
xs)

foldMapA :: (Applicative f, Traversable t, Monoid m) => (a -> f m) -> t a -> f m
foldMapA :: (a -> f m) -> t a -> f m
foldMapA = ((t m -> m) -> f (t m) -> f m
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap t m -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (f (t m) -> f m) -> (t a -> f (t m)) -> t a -> f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((t a -> f (t m)) -> t a -> f m)
-> ((a -> f m) -> t a -> f (t m)) -> (a -> f m) -> t a -> f m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f m) -> t a -> f (t m)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse