extra-0.3: Extra functions I use.

Safe HaskellSafe-Inferred

Control.Monad.Extra

Description

Extra functions for Control.Exception. These functions provide looping, list operations and booleans. If you need a wider selection of monad loops and list generalisations, see http://hackage.haskell.org/package/monad-loops

Synopsis

Documentation

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

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

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

unit :: m () -> m ()Source

The identity function which requires the inner argument to be '()'. Useful for functions with overloaded return times.

 \(x :: Maybe ()) -> unit x == x

partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])Source

A version of partition that works with a monadic predicate.

 partitionM (Just . even) [1,2,3] == Just ([2], [1,3])
 partitionM (const Nothing) [1,2,3] == Nothing

concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]Source

A version of concatMap that works with a monadic predicate.

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]Source

A version of mapMaybe that works with a monadic predicate.

loopM :: Monad m => (a -> m (Either a b)) -> a -> m bSource

A looping operation, where the predicate returns Left as a seed for the next loop or Right to abort the loop.

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

Keep running an operation until it becomes False.

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

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

ifM :: Monad m => m Bool -> m a -> m a -> m aSource

notM :: Functor m => m Bool -> m BoolSource

(||^) :: Monad m => m Bool -> m Bool -> m BoolSource

(&&^) :: Monad m => m Bool -> m Bool -> m BoolSource

orM :: Monad m => [m Bool] -> m BoolSource

andM :: Monad m => [m Bool] -> m BoolSource

anyM :: Monad m => (a -> m Bool) -> [a] -> m BoolSource

allM :: Monad m => (a -> m Bool) -> [a] -> m BoolSource

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

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