diff options
| author | Joey Hess <joey@kitenet.net> | 2014-05-14 19:41:05 -0400 |
|---|---|---|
| committer | Joey Hess <joey@kitenet.net> | 2014-05-14 19:41:05 -0400 |
| commit | 7115d1ec162b4059b3e8e8f84bd8d5898c1db025 (patch) | |
| tree | 42c1cce54e890e1d56484794ab33129132d8fee2 /Utility/Monad.hs | |
| parent | ffe371a9d42cded461236e972a24a142419d7fc4 (diff) | |
moved source code to src
This is to work around OSX's brain-damange regarding filename case
insensitivity.
Avoided moving config.hs, because it's a config file. Put in a symlink to
make build work.
Diffstat (limited to 'Utility/Monad.hs')
| -rw-r--r-- | Utility/Monad.hs | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/Utility/Monad.hs b/Utility/Monad.hs deleted file mode 100644 index eba3c428..00000000 --- a/Utility/Monad.hs +++ /dev/null @@ -1,69 +0,0 @@ -{- monadic stuff - - - - Copyright 2010-2012 Joey Hess <joey@kitenet.net> - - - - License: BSD-2-clause - -} - -module Utility.Monad where - -import Data.Maybe -import Control.Monad - -{- Return the first value from a list, if any, satisfying the given - - predicate -} -firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a) -firstM _ [] = return Nothing -firstM p (x:xs) = ifM (p x) (return $ Just x , firstM p xs) - -{- Runs the action on values from the list until it succeeds, returning - - its result. -} -getM :: Monad m => (a -> m (Maybe b)) -> [a] -> m (Maybe b) -getM _ [] = return Nothing -getM p (x:xs) = maybe (getM p xs) (return . Just) =<< p x - -{- 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 p = liftM isJust . firstM p - -allM :: Monad m => (a -> m Bool) -> [a] -> m Bool -allM _ [] = return True -allM p (x:xs) = p x <&&> allM p xs - -{- Runs an action on values from a list until it succeeds. -} -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 - -{- short-circuiting monadic || -} -(<||>) :: Monad m => m Bool -> m Bool -> m Bool -ma <||> mb = ifM ma ( return True , mb ) - -{- short-circuiting monadic && -} -(<&&>) :: Monad m => m Bool -> m Bool -> m Bool -ma <&&> mb = ifM ma ( mb , return False ) - -{- Same fixity as && and || -} -infixr 3 <&&> -infixr 2 <||> - -{- 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 - 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 = observe . const - -{- do nothing -} -noop :: Monad m => m () -noop = return () |
