summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Feuer <David.Feuer@gmail.com>2014-10-01 15:59:39 +0200
committerJoachim Breitner <mail@joachim-breitner.de>2014-10-01 16:00:30 +0200
commit18dc7324765b04781158d2a97b6bdbd07812bff4 (patch)
treea66cdf4e3f7ea9b5a03d280948786634ffd9a4f3
parent1bc83b8a3d5ac01357d42b14d513312e98e2bb29 (diff)
downloadhaskell-wip/validate-T9546.tar.gz
Make filterM a good consumerwip/validate-T9546
analogously to mapM. Fixes #9546.
-rw-r--r--libraries/base/Control/Monad.hs12
-rw-r--r--libraries/base/changelog.md3
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*