diff options
author | Joey Hess <joey@kitenet.net> | 2012-03-16 12:28:17 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-03-16 12:28:17 -0400 |
commit | 771052a85e3a000911e8a438012e61b3caf9c1a8 (patch) | |
tree | 068b19c71758dca9ec76a1d07c8c291806419765 /Utility/Monad.hs | |
parent | b06336fa3a146e9a0ef1a1307b1fc219570795c6 (diff) |
optimize monadic ||
(||) used applicative style runs both conditions rather than short
circuiting. Add an orM that properly short-circuits.
Diffstat (limited to 'Utility/Monad.hs')
-rw-r--r-- | Utility/Monad.hs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/Utility/Monad.hs b/Utility/Monad.hs index 23c0c4c19..5cc243290 100644 --- a/Utility/Monad.hs +++ b/Utility/Monad.hs @@ -8,7 +8,7 @@ module Utility.Monad where import Data.Maybe -import Control.Monad (liftM) +import Control.Monad (liftM, liftM2) {- Return the first value from a list, if any, satisfying the given - predicate -} @@ -31,6 +31,16 @@ ifM cond (thenclause, elseclause) = do c <- cond if c then thenclause else elseclause +{- monadic || + - + - Compare with (||) <$> ma <*> mb, which always runs both ma and mb. -} +orM :: Monad m => m Bool -> m Bool -> m Bool +orM ma mb = ifM ma ( return True , mb ) + +{- monadic && (for completeness) -} +andM :: Monad m => m Bool -> m Bool -> m Bool +andM = liftM2 (&&) + {- 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 |