{-| 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 ()))