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