module Data.Fold.Common.M where
import Prelude hiding (any, all)
import Data.Fold
import Data.Fold.Internal hiding (First)
import Data.Monoid
any :: (a -> Bool) -> M a Bool
any p = M getAny (Any . p) (<>) (Any False)
all :: (a -> Bool) -> M a Bool
all p = M getAll (All . p) (<>) (All False)
and :: M Bool Bool
and = all id
or :: M Bool Bool
or = any id
find :: (a -> Bool) -> M a (Maybe a)
find p = M getFirst to (<>) (First Nothing)
where to a = First $ if p a then Just a else Nothing
indexOf :: Enum e => (a -> Bool) -> M a (Maybe e)
indexOf p = M (maybe' Nothing Just) to m Nothing'
where to a = if p a then Just' (toEnum 0) else Nothing'
m (Just' a) _ = Just' (succ a)
m _ (Just' a) = Just' (succ a)
m _ _ = Nothing'
head :: M a (Maybe a)
head = M getFirst (First . Just) (<>) (First Nothing)
strictify :: M a b -> L' a b
strictify (M p to m z) = L' p (\z a -> z `m` to a) z