Skip to content

Commit

Permalink
Merge pull request #96 from purescript/whenm
Browse files Browse the repository at this point in the history
Add "whenM" and "unlessM"
  • Loading branch information
garyb authored Sep 30, 2016
2 parents 4b9fdde + 57edbbf commit a1e2e76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Control/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Control.Monad
( class Monad
, liftM1
, ap
, whenM
, unlessM
, module Data.Functor
, module Control.Apply
, module Control.Applicative
Expand All @@ -13,6 +15,7 @@ import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit)

-- | The `Monad` type class combines the operations of the `Bind` and
-- | `Applicative` type classes. Therefore, `Monad` instances represent type
Expand Down Expand Up @@ -61,3 +64,17 @@ ap f a = do
f' <- f
a' <- a
pure (f' a')

-- | Perform a monadic action when a condition is true, where the conditional
-- | value is also in a monadic context.
whenM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
whenM mb m = do
b <- mb
when b m

-- | Perform a monadic action unless a condition is true, where the conditional
-- | value is also in a monadic context.
unlessM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
unlessM mb m = do
b <- mb
unless b m
2 changes: 1 addition & 1 deletion src/Prelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Control.Applicative (class Applicative, pure, liftA1, unless, when)
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))
import Control.Category (class Category, id)
import Control.Monad (class Monad, ap, liftM1)
import Control.Monad (class Monad, ap, liftM1, unlessM, whenM)
import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))

import Data.Boolean (otherwise)
Expand Down

0 comments on commit a1e2e76

Please sign in to comment.