X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Futils%2FMonadUtils.hs;h=dc54620c20e029f641c9b0084097517f6347d9da;hp=5e01a222b21bdb917853c6c416d5175efbeb98e1;hb=a5b8386a8ce31296c3b748a965f54b0c16948c09;hpb=12da626e907fd4b43272d7fa9a98ebc1a4bb0ebe diff --git a/compiler/utils/MonadUtils.hs b/compiler/utils/MonadUtils.hs index 5e01a22..dc54620 100644 --- a/compiler/utils/MonadUtils.hs +++ b/compiler/utils/MonadUtils.hs @@ -19,6 +19,7 @@ module MonadUtils , mapSndM , concatMapM , mapMaybeM + , fmapMaybeM, fmapEitherM , 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 +-- | 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