Commit 41fe4e3e authored by jon.fairbairn@cl.cam.ac.uk's avatar jon.fairbairn@cl.cam.ac.uk
Browse files

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.
parent 6583bc7b
......@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment