added ifM and nuked 11 lines of code

no behavior changes
This commit is contained in:
Joey Hess 2012-03-14 17:43:34 -04:00
parent a4f72c9625
commit 60ab3d84e1
17 changed files with 151 additions and 162 deletions

View file

@ -1,6 +1,6 @@
{- monadic stuff
-
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
@ -14,11 +14,7 @@ import Control.Monad (liftM)
- predicate -}
firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
firstM _ [] = return Nothing
firstM p (x:xs) = do
q <- p x
if q
then return (Just x)
else firstM p xs
firstM p (x:xs) = ifM (p x) (return $ Just x , firstM p xs)
{- Returns true if any value in the list satisfies the predicate,
- stopping once one is found. -}
@ -29,6 +25,12 @@ anyM p = liftM isJust . firstM p
untilTrue :: Monad m => [a] -> (a -> m Bool) -> m Bool
untilTrue = flip anyM
{- if with a monadic conditional. -}
ifM :: Monad m => m Bool -> (m a, m a) -> m a
ifM cond (thenclause, elseclause) = do
c <- cond
if c then thenclause else elseclause
{- Runs an action, passing its value to an observer before returning it. -}
observe :: Monad m => (a -> m b) -> m a -> m a
observe observer a = do