module Precursor.Control.Alternative
(
Alternative
, (<|>)
, empty
, some
, many
, optional
, afilter
, asum
, guard
, ensure
, eitherA
, toAlt
, mapAlt
) where
import Precursor.Control.Applicative
import Precursor.Data.Bool
import Precursor.Control.Category
import Control.Applicative
import Control.Monad (guard)
import Data.Foldable (asum)
import Data.Monoid (getAlt)
import Precursor.Data.Either
import Precursor.Structure.Foldable
import Precursor.Control.Functor
import Precursor.Control.Monad
afilter :: (Monad m, Alternative m) => (a -> Bool) -> m a -> m a
afilter p = (=<<) (\x -> bool (pure x) empty (p x))
ensure :: (Alternative f) => (a -> Bool) -> a -> f a
ensure p x = x <$ guard (p x)
eitherA :: Alternative f => f a -> f b -> f (Either a b)
eitherA x y = fmap Left x <|> fmap Right y
toAlt :: (Alternative f, Foldable t) => t a -> f a
toAlt = getAlt . foldMap pure
mapAlt :: (Monad m, Alternative m, Foldable f) => (a -> f b) -> m a -> m b
mapAlt f = (=<<) (toAlt . f)