Skip to content

Commit

Permalink
Merge pull request #18 from pseudonom/master
Browse files Browse the repository at this point in the history
Added filterM
  • Loading branch information
paf31 committed Mar 19, 2015
2 parents 9a4953b + b17994e commit 65da234
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@ unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
```


#### `filterM`

``` purescript
filterM :: forall a m. (Monad m) => (a -> m Boolean) -> [a] -> m [a]
```

Filter where the predicate returns a monadic Boolean. For example:

```purescript
powerSet :: forall a. [a] -> [[a]]
powerSet = filterM (const [true, false])
```


## Module Control.MonadPlus

Expand Down
15 changes: 15 additions & 0 deletions src/Control/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ when false _ = return unit
unless :: forall m. (Monad m) => Boolean -> m Unit -> m Unit
unless false m = m
unless true _ = return unit

-- | Filter where the predicate returns a monadic Boolean. For example:
-- |
-- | ```purescript
-- | powerSet :: forall a. [a] -> [[a]]
-- | powerSet = filterM (const [true, false])
-- | ```
filterM :: forall a m. (Monad m) => (a -> m Boolean) -> [a] -> m [a]
filterM _ [] = return []
filterM p (x:xs) = do
b <- p x
xs' <- filterM p xs
return $ if b
then x : xs'
else xs'

0 comments on commit 65da234

Please sign in to comment.