extra-1.4.2: Extra functions I use.

Safe Haskell | Safe
Language | Haskell2010

Control.Monad.Extra

Contents

- Loops
- Lists
- Booleans

Description

Extra functions for Control.Monad. These functions provide looping, list operations and booleans. If you need a wider selection of monad loops and list generalisations, see monad-loops.

Synopsis

- module Control.Monad
- whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
- whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
- unit :: m () -> m ()
- loopM :: Monad m => (a -> m (Either a b)) -> a -> m b
- whileM :: Monad m => m Bool -> m ()
- partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
- concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
- mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
- findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
- firstJustM :: Monad m => (a -> m (Maybe b)) -> [a] -> m (Maybe b)
- whenM :: Monad m => m Bool -> m () -> m ()
- unlessM :: Monad m => m Bool -> m () -> m ()
- ifM :: Monad m => m Bool -> m a -> m a -> m a
- notM :: Functor m => m Bool -> m Bool
- (||^) :: Monad m => m Bool -> m Bool -> m Bool
- (&&^) :: Monad m => m Bool -> m Bool -> m Bool
- orM :: Monad m => [m Bool] -> m Bool
- andM :: Monad m => [m Bool] -> m Bool
- anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
- allM :: Monad m => (a -> m Bool) -> [a] -> m Bool

# Documentation

module Control.Monad

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

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

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

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

Like `whenJust`, but where the test can be monadic.

unit :: m () -> m ()

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

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

# Loops

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

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

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`.

# Lists

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

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]

A version of `concatMap` that works with a monadic predicate.

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

A version of `mapMaybe` that works with a monadic 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")

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.

# Booleans

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

Like `when`, but where the test can be monadic.

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

Like `unless`, but where the test can be monadic.

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

Like `if`, but where the test can be monadic.

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

Like `not`, but where the test can be monadic.

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

The lazy `||` operator lifted to a monad. If the first argument evaluates to `True` the second argument will not be evaluated.

    Just True  ||^ undefined  == Just True
    Just False ||^ Just True  == Just True
    Just False ||^ Just False == Just False

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

The lazy `&&` operator lifted to a monad. If the first argument evaluates to `False` the second argument will not be evaluated.

    Just False &&^ undefined  == Just False
    Just True  &&^ Just True  == Just True
    Just True  &&^ Just False == Just False

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

A version of `or` lifted to a monad. Retains the short-circuiting behaviour.

    orM [Just False,Just True ,undefined] == Just True
    orM [Just False,Just False,undefined] == undefined
    \xs -> Just (or xs) == orM (map Just xs)

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

A version of `and` lifted to a monad. Retains the short-circuiting behaviour.

    andM [Just True,Just False,undefined] == Just False
    andM [Just True,Just True ,undefined] == undefined
    \xs -> Just (and xs) == andM (map Just xs)

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

A version of `any` lifted to a monad. Retains the short-circuiting behaviour.

    anyM Just [False,True ,undefined] == Just True
    anyM Just [False,False,undefined] == undefined
    \(f :: Int -> Maybe Bool) xs -> anyM f xs == orM (map f xs)

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

A version of `all` lifted to a monad. Retains the short-circuiting behaviour.

    allM Just [True,False,undefined] == Just False
    allM Just [True,True ,undefined] == undefined
    \(f :: Int -> Maybe Bool) xs -> anyM f xs == orM (map f xs)