diff options
author | Richard Lupton <richard.lupton@gmail.com> | 2019-08-11 12:06:00 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2019-08-19 02:11:59 -0400 |
commit | ac7c738b19d626a827b688b6bac28c9beb28541d (patch) | |
tree | bd368d274d29f2ee62667525d928ea5eb2a599ed | |
parent | 5b713aa3d0159f5190e197e57765195a98ce9520 (diff) | |
download | haskell-ac7c738b19d626a827b688b6bac28c9beb28541d.tar.gz |
Generalized MonadUtils folds to Foldable (#16969)
-rw-r--r-- | compiler/utils/MonadUtils.hs | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/compiler/utils/MonadUtils.hs b/compiler/utils/MonadUtils.hs index 5c28aa5f39..936ae62751 100644 --- a/compiler/utils/MonadUtils.hs +++ b/compiler/utils/MonadUtils.hs @@ -32,7 +32,7 @@ import Control.Applicative import Control.Monad import Control.Monad.Fix import Control.Monad.IO.Class -import Data.Foldable (sequenceA_) +import Data.Foldable (sequenceA_, foldr) import Data.List (unzip4, unzip5, zipWith4) ------------------------------------------------------------------------------- @@ -191,17 +191,16 @@ orM :: Monad m => m Bool -> m Bool -> m Bool orM m1 m2 = m1 >>= \x -> if x then return True else m2 -- | Monadic version of foldl -foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a +foldlM :: (Monad m, Foldable t) => (a -> b -> m a) -> a -> t b -> m a foldlM = foldM -- | Monadic version of foldl that discards its result -foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m () +foldlM_ :: (Monad m, Foldable t) => (a -> b -> m a) -> a -> t b -> m () foldlM_ = foldM_ -- | Monadic version of foldr -foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a -foldrM _ z [] = return z -foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r } +foldrM :: (Monad m, Foldable t) => (b -> a -> m a) -> a -> t b -> m a +foldrM k z x = foldr (\x r -> r >>= k x) (pure z) x -- | Monadic version of fmap specialised for Maybe maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b)) |