projects
/
ghc-hetmet.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
12da626
)
Add fmapMaybeM and fmapEitherM
author
simonpj@microsoft.com
<unknown>
Thu, 4 Mar 2010 12:40:04 +0000
(12:40 +0000)
committer
simonpj@microsoft.com
<unknown>
Thu, 4 Mar 2010 12:40:04 +0000
(12:40 +0000)
compiler/utils/MonadUtils.hs
patch
|
blob
|
history
diff --git
a/compiler/utils/MonadUtils.hs
b/compiler/utils/MonadUtils.hs
index
5e01a22
..
dc54620
100644
(file)
--- a/
compiler/utils/MonadUtils.hs
+++ b/
compiler/utils/MonadUtils.hs
@@
-19,6
+19,7
@@
module MonadUtils
, mapSndM
, concatMapM
, mapMaybeM
, mapSndM
, concatMapM
, mapMaybeM
+ , fmapMaybeM, fmapEitherM
, anyM, allM
, foldlM, foldlM_, foldrM
, maybeMapM
, anyM, allM
, foldlM, foldlM_, foldrM
, maybeMapM
@@
-148,6
+149,16
@@
concatMapM f xs = liftM concat (mapM f xs)
mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM f = liftM catMaybes . mapM f
mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM f = liftM catMaybes . mapM f
+-- | Monadic version of fmap
+fmapMaybeM :: (Monad m) => (a -> m b) -> Maybe a -> m (Maybe b)
+fmapMaybeM _ Nothing = return Nothing
+fmapMaybeM f (Just x) = f x >>= (return . Just)
+
+-- | Monadic version of fmap
+fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
+fmapEitherM fl _ (Left a) = fl a >>= (return . Left)
+fmapEitherM _ fr (Right b) = fr b >>= (return . Right)
+
-- | Monadic version of 'any', aborts the computation at the first @True@ value
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyM _ [] = return False
-- | Monadic version of 'any', aborts the computation at the first @True@ value
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyM _ [] = return False