diff options
author | Joey Hess <joey@kitenet.net> | 2012-03-14 17:43:34 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-03-14 17:43:34 -0400 |
commit | 60ab3d84e188b8dd3a284d962df25bbee41ff1cb (patch) | |
tree | 768d4f632bab0152dbc1ca72f81fc3b9c7915c0a /Utility/Monad.hs | |
parent | a4f72c9625486786a4549cf4db1b542ea89da7c7 (diff) |
added ifM and nuked 11 lines of code
no behavior changes
Diffstat (limited to 'Utility/Monad.hs')
-rw-r--r-- | Utility/Monad.hs | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Utility/Monad.hs b/Utility/Monad.hs index 28aa33ee8..23c0c4c19 100644 --- a/Utility/Monad.hs +++ b/Utility/Monad.hs @@ -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 |