From 814570f739399a92cf181e12660e610679a2d801 Mon Sep 17 00:00:00 2001 From: David Feuer Date: Tue, 30 Dec 2014 11:42:39 -0500 Subject: [PATCH] Add filterM Thus far, this proposal has gotten good reception on the libraries list. --- Data/Sequence.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Data/Sequence.hs b/Data/Sequence.hs index 0a64c3e85..094046b40 100644 --- a/Data/Sequence.hs +++ b/Data/Sequence.hs @@ -130,6 +130,7 @@ module Data.Sequence ( -- * Transformations mapWithIndex, -- :: (Int -> a -> b) -> Seq a -> Seq b reverse, -- :: Seq a -> Seq a + filterM, -- :: Applicative f => (a -> f Bool) -> Seq a -> f (Seq a) -- ** Zips zip, -- :: Seq a -> Seq b -> Seq (a, b) zipWith, -- :: (a -> b -> c) -> Seq a -> Seq b -> Seq c @@ -488,6 +489,12 @@ thin12 (Deep s pr m (Three a b c)) = Deep s pr (thin $ m `snocTree` node2 a b) ( thin12 (Deep s pr m (Four a b c d)) = Deep s pr (thin $ m `snocTree` node2 a b) (Two c d) thin12 _ = error "thin12 expects a Deep FingerTree." +filterM :: Applicative f => (a -> f Bool) -> Seq a -> f (Seq a) +filterM p = foldr go (pure empty) + where + go x r = f <$> p x <*> r + where + f flg ys = if flg then x <| ys else ys instance MonadPlus Seq where mzero = empty