relude-0.1.0: Custom prelude from Kowainik

Safe HaskellSafe
LanguageHaskell2010

Relude.Bool.Guard

Description

This module contains monadic predicates.

Synopsis

Documentation

guardM :: MonadPlus m => m Bool -> m () Source #

Monadic version of guard. Occasionally useful. Here some complex but real-life example:

findSomePath :: IO (Maybe FilePath)

somePath :: MaybeT IO FilePath
somePath = do
    path <- MaybeT findSomePath
    guardM $ liftIO $ doesDirectoryExist path
    return path

ifM :: Monad m => m Bool -> m a -> m a -> m a Source #

Monadic version of if-then-else.

>>> ifM (pure True) (putTextLn "True text") (putTextLn "False text")
True text

unlessM :: Monad m => m Bool -> m () -> m () Source #

Monadic version of unless.

>>> unlessM (pure False) $ putTextLn "No text :("
No text :(
>>> unlessM (pure True) $ putTextLn "Yes text :)"

whenM :: Monad m => m Bool -> m () -> m () Source #

Monadic version of when.

>>> whenM (pure False) $ putTextLn "No text :("
>>> whenM (pure True)  $ putTextLn "Yes text :)"
Yes text :)
>>> whenM (Just True) (pure ())
Just ()
>>> whenM (Just False) (pure ())
Just ()
>>> whenM Nothing (pure ())
Nothing