wren-extras-2011.7.13: wren ng thornton's random collection of extras

Portabilityportable
Stabilityexperimental
Maintainerwren@community.haskell.org

Control.Monad.Extras

Contents

Description

Some basic monad combinators that Control.Monad lacks. Many of these functions have been adopted by the IfElse package: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/IfElse

Synopsis

Monadic conditionals

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

Choose a computation to run based on the boolean.

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

If the boolean is true, then run the computation.

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

If the boolean is false, then run the computation.

Looping constructs

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

Execute a monadic action so long as a monadic boolean returns true.

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

Negation of whileM: execute an action so long as the boolean returns false.

Specialized returns

return' :: Monad m => a -> m aSource

Strict version of return because usually we don't need that extra thunk.

returning :: Monad m => (a -> m b) -> a -> m aSource

Take an action and make it into a side-effecting return. Because I seem to keep running into m () and the like.

This is the moral inverse of Control.Monad.void :: Functor f => f a -> f () which is added in base-4.3.0.0.

Other functions

liftM' :: Monad m => (a -> b) -> m a -> m bSource

Strict version of liftM. Useful, for example, to make foldM strict which can be necessary to avoid stack overflows.

maybeMP :: MonadPlus m => Maybe a -> m aSource

This conversion is common enough to make a name for.

maybeEither :: a -> Maybe b -> Either a bSource

A variant of maybe (or either) for converting the Maybe monad into the (Either a) monad, i.e. for naming your errors.