]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Lib/Control/Monad.hs
polish code
[haskell/symantic.git] / Language / Symantic / Lib / Control / Monad.hs
1 module Language.Symantic.Lib.Control.Monad where
2
3 import Control.Monad (liftM2, liftM3, liftM4, join)
4
5 -- * 'Monad'ic utilities
6
7 -- | Perform some operation on 'Just', given the field inside the 'Just'.
8 whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
9 whenJust mg f = maybe (pure ()) f mg
10
11 -- | Like 'when', but where the test can be 'Monad'-ic.
12 whenM :: Monad m => m Bool -> m () -> m ()
13 whenM b t = ifM b t (return ())
14
15 -- | Like 'unless', but where the test can be 'Monad'-ic.
16 unlessM :: Monad m => m Bool -> m () -> m ()
17 unlessM b = ifM b (return ())
18
19 -- | Like @if@, but where the test can be 'Monad'-ic.
20 ifM :: Monad m => m Bool -> m a -> m a -> m a
21 ifM b t f = do b' <- b; if b' then t else f
22
23 -- | Like 'liftM' but 'join' the result of the lifted function.
24 liftMJoin :: Monad m => (a -> m b) -> m a -> m b
25 liftMJoin = (=<<)
26
27 -- | Like 'liftM2' but 'join' the result of the lifted function.
28 liftM2Join :: Monad m => (a -> b -> m c) -> m a -> m b -> m c
29 liftM2Join f ma mb = join (liftM2 f ma mb)
30
31 -- | Like 'liftM3' but 'join' the result of the lifted function.
32 liftM3Join :: Monad m => (a -> b -> c -> m d) -> m a -> m b -> m c -> m d
33 liftM3Join f ma mb mc = join (liftM3 f ma mb mc)
34
35 -- | Like 'liftM3' but 'join' the result of the lifted function.
36 liftM4Join :: Monad m => (a -> b -> c -> d -> m e) -> m a -> m b -> m c -> m d -> m e
37 liftM4Join f ma mb mc md = join (liftM4 f ma mb mc md)