From 92b2ffdc5d0031aa55f4cebb93536e88b6bf6dc3 Mon Sep 17 00:00:00 2001 From: "jon.fairbairn@cl.cam.ac.uk" Date: Thu, 17 Sep 2009 14:56:16 +0000 Subject: [PATCH] Add mfilter to Control.Monad Straightforward MonadPlus version of List.filter. I would prefer to call it filter, but the current naming scheme for Control.Monad implies mfilter. --- Control/Monad.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Control/Monad.hs b/Control/Monad.hs index 5b46f8f..bf94ad4 100644 --- a/Control/Monad.hs +++ b/Control/Monad.hs @@ -46,6 +46,7 @@ module Control.Monad , join -- :: (Monad m) => m (m a) -> m a , msum -- :: (MonadPlus m) => [m a] -> m a + , mfilter -- :: (MonadPlus m) => (a -> Bool) -> m a -> m a , filterM -- :: (Monad m) => (a -> m Bool) -> [a] -> m [a] , mapAndUnzipM -- :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c]) , zipWithM -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c] @@ -311,6 +312,20 @@ ap :: (Monad m) => m (a -> b) -> m a -> m b ap = liftM2 id +-- ----------------------------------------------------------------------------- +-- Other MonadPlus functions + +-- | Direct 'MonadPlus' equivalent of 'filter' +-- @'filter'@ = @(mfilter:: (a -> Bool) -> [a] -> [a]@ +-- applicable to any 'MonadPlus', for example +-- @mfilter odd (Just 1) == Just 1@ +-- @mfilter odd (Just 2) == Nothing@ + +mfilter :: (MonadPlus m) => (a -> Bool) -> m a -> m a +mfilter p ma = do + a <- ma + if p a then return a else mzero + {- $naming The functions in this library use the following naming conventions: -- 1.7.10.4