This commit is contained in:
Joey Hess 2012-01-21 02:24:12 -04:00
parent eb9001044f
commit 183bdacca2
4 changed files with 12 additions and 8 deletions

View file

@ -12,7 +12,7 @@ import Control.Monad (liftM)
{- Return the first value from a list, if any, satisfying the given
- predicate -}
firstM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a)
firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
firstM _ [] = return Nothing
firstM p (x:xs) = do
q <- p x
@ -22,20 +22,20 @@ firstM p (x:xs) = do
{- Returns true if any value in the list satisfies the predicate,
- stopping once one is found. -}
anyM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyM p = liftM isJust . firstM p
{- Runs an action on values from a list until it succeeds. -}
untilTrue :: (Monad m) => [a] -> (a -> m Bool) -> m Bool
untilTrue :: Monad m => [a] -> (a -> m Bool) -> m Bool
untilTrue = flip anyM
{- Runs an action, passing its value to an observer before returning it. -}
observe :: (Monad m) => (a -> m b) -> m a -> m a
observe :: Monad m => (a -> m b) -> m a -> m a
observe observer a = do
r <- a
_ <- observer r
return r
{- b `after` a runs first a, then b, and returns the value of a -}
after :: (Monad m) => m b -> m a -> m a
after :: Monad m => m b -> m a -> m a
after = observe . const