prelate-0.7.0.1: A Prelude
Safe HaskellSafe-Inferred
LanguageGHC2021

Prelate.Control.Monad

Description

Monad reexports from extra and some combinators

Synopsis

Documentation

tap :: Functor m => (a -> m ()) -> a -> m a Source #

Call a side-effecting function on a value and return the value.

findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a) #

Like find, but where the test can be monadic.

findM (Just . isUpper) "teST"             == Just (Just 'S')
findM (Just . isUpper) "test"             == Just Nothing
findM (Just . const True) ["x",undefined] == Just (Just "x")

firstJustM :: Monad m => (a -> m (Maybe b)) -> [a] -> m (Maybe b) #

Like findM, but also allows you to compute some additional information in the predicate.

whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m () #

Perform some operation on Just, given the field inside the Just. This is a specialized for_.

whenJust Nothing  print == pure ()
whenJust (Just 1) print == print 1

whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m () #

Like whenJust, but where the test can be monadic.

pureIf :: Alternative m => Bool -> a -> m a #

Return either a pure value if a condition is True, otherwise empty.

pureIf @Maybe True  5 == Just 5
pureIf @Maybe False 5 == Nothing
pureIf @[]    True  5 == [5]
pureIf @[]    False 5 == []

whenMaybe :: Applicative m => Bool -> m a -> m (Maybe a) #

Like when, but return either Nothing if the predicate was False, of Just with the result of the computation.

whenMaybe True  (print 1) == fmap Just (print 1)
whenMaybe False (print 1) == pure Nothing

whenMaybeM :: Monad m => m Bool -> m a -> m (Maybe a) #

Like whenMaybe, but where the test can be monadic.

whileM :: Monad m => m Bool -> m () #

Keep running an operation until it becomes False. As an example:

whileM $ do sleep 0.1; notM $ doesFileExist "foo.txt"
readFile "foo.txt"

If you need some state persisted between each test, use loopM.

whileJustM :: (Monad m, Monoid a) => m (Maybe a) -> m a #

Keep running an operation until it becomes a Nothing, accumulating the monoid results inside the Justs as the result of the overall loop.

untilJustM :: Monad m => m (Maybe a) -> m a #

Keep running an operation until it becomes a Just, then return the value inside the Just as the result of the overall loop.