prelate-0.3.0.0: A Prelude
Safe HaskellSafe-Inferred
LanguageHaskell2010

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.

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.

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")

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.

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.

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.

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

Like whenMaybe, but where the test can be monadic.

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

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 == []

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

Like whenJust, but where the test can be monadic.

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

Perform some operation on Just, given the field inside the Just.

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