Skip to content

Commit a1e2e76

Browse files
authored
Merge pull request #96 from purescript/whenm
Add "whenM" and "unlessM"
2 parents 4b9fdde + 57edbbf commit a1e2e76

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Control/Monad.purs

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Control.Monad
22
( class Monad
33
, liftM1
44
, ap
5+
, whenM
6+
, unlessM
57
, module Data.Functor
68
, module Control.Apply
79
, module Control.Applicative
@@ -13,6 +15,7 @@ import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
1315
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))
1416

1517
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
18+
import Data.Unit (Unit)
1619

1720
-- | The `Monad` type class combines the operations of the `Bind` and
1821
-- | `Applicative` type classes. Therefore, `Monad` instances represent type
@@ -61,3 +64,17 @@ ap f a = do
6164
f' <- f
6265
a' <- a
6366
pure (f' a')
67+
68+
-- | Perform a monadic action when a condition is true, where the conditional
69+
-- | value is also in a monadic context.
70+
whenM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
71+
whenM mb m = do
72+
b <- mb
73+
when b m
74+
75+
-- | Perform a monadic action unless a condition is true, where the conditional
76+
-- | value is also in a monadic context.
77+
unlessM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
78+
unlessM mb m = do
79+
b <- mb
80+
unless b m

src/Prelude.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Control.Applicative (class Applicative, pure, liftA1, unless, when)
3030
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
3131
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))
3232
import Control.Category (class Category, id)
33-
import Control.Monad (class Monad, ap, liftM1)
33+
import Control.Monad (class Monad, ap, liftM1, unlessM, whenM)
3434
import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))
3535

3636
import Data.Boolean (otherwise)

0 commit comments

Comments
 (0)