summaryrefslogtreecommitdiff
path: root/src/Utils/Monad.hs
blob: 3c1ad17468c8eb72b90bbe290f393cf0b3177f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{-|
Module      : Utils.Monad
Copyright   : (c) UU, 2019
License     : MIT
Maintainer  : Tom Smeding
Stability   : experimental
Portability : POSIX, macOS, Windows

Some useful functions lifted to monadic actions.
-}
module Utils.Monad where


-- | If the boolean resolves to True, returns the first argument, else the
-- second.
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM b t e = b >>= \b' -> if b' then t else e

-- | Runs the monadic action only if the boolean resolves to True.
whenM :: Monad m => m Bool -> m () -> m ()
whenM b t = ifM b t (return ())

-- | whenJust m a = when (isJust m) (a (fromJust m)), but with less
-- fromJust.
whenJust :: Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust = flip (maybe (return ()))