1 {-# OPTIONS_GHC -fno-warn-tabs #-}
2 module Hcompta.Lib.Control.Monad where
4 import Control.Applicative (Applicative(..))
5 import Control.Monad (Monad(..), (=<<), liftM2, liftM3, liftM4, join)
7 import Data.Maybe (Maybe(..), maybe)
9 -- * 'Monad'ic utilities
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
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 ())
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 ())
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
27 -- | Like 'liftM' but 'join' the result of the lifted function.
28 liftMJoin :: Monad m => (a -> m b) -> m a -> m b
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)
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)
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)