diff options
author | David Feuer <David.Feuer@gmail.com> | 2014-10-01 15:59:39 +0200 |
---|---|---|
committer | Joachim Breitner <mail@joachim-breitner.de> | 2014-10-01 18:20:26 +0200 |
commit | 96a4062a7e7587592829c045b3b12c755cc8e329 (patch) | |
tree | d4c0210252e6b9ff4a83d0a28e3d680cc2cc9ff1 | |
parent | 789321098f86fd3c4483b24372f8938f89b12312 (diff) | |
download | haskell-96a4062a7e7587592829c045b3b12c755cc8e329.tar.gz |
Make filterM a good consumer
analogously to mapM. Fixes #9546.
-rw-r--r-- | libraries/base/Control/Monad.hs | 12 | ||||
-rw-r--r-- | libraries/base/changelog.md | 3 |
2 files changed, 9 insertions, 6 deletions
diff --git a/libraries/base/Control/Monad.hs b/libraries/base/Control/Monad.hs index 619a2bad94..db46dea699 100644 --- a/libraries/base/Control/Monad.hs +++ b/libraries/base/Control/Monad.hs @@ -93,12 +93,14 @@ guard False = empty -- | This generalizes the list-based 'filter' function. +{-# INLINE filterM #-} filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a] -filterM _ [] = return [] -filterM p (x:xs) = do - flg <- p x - ys <- filterM p xs - return (if flg then x:ys else ys) +filterM p = foldr go (return []) + where + go x r = do + flg <- p x + ys <- r + return (if flg then x:ys else ys) infixr 1 <=<, >=> diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md index 09b749afd6..f7d8b1ca68 100644 --- a/libraries/base/changelog.md +++ b/libraries/base/changelog.md @@ -77,7 +77,8 @@ second argument, so that the fusion RULES for it do not change the semantics. (#9596) - * `scanr` and `mapAccumL` now take part in list fusion (#9355, #9502) + * `scanr`, `mapAccumL` and `filterM` now take part in list fusion (#9355, + #9502, #9546) ## 4.7.0.1 *Jul 2014* |