From 18dc7324765b04781158d2a97b6bdbd07812bff4 Mon Sep 17 00:00:00 2001 From: David Feuer Date: Wed, 1 Oct 2014 15:59:39 +0200 Subject: Make filterM a good consumer analogously to mapM. Fixes #9546. --- libraries/base/Control/Monad.hs | 12 +++++++----- 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* -- cgit v1.2.1