universum-1.2.0: Custom prelude used in Serokell

Safe HaskellNone
LanguageHaskell2010

Universum.Monad.Container

Description

This module exports functions which allow to process instances of Container type class in monadic way.

Synopsis

Documentation

concatMapM :: (Applicative f, Monoid m, Container (l m), Element (l m) ~ m, Traversable l) => (a -> f m) -> l a -> f m Source #

Lifting bind into a monad. Generalized version of concatMap that works with a monadic predicate. Old and simpler specialized to list version had next type:

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

Side note: previously it had type

concatMapM :: (Applicative q, Monad m, Traversable m)
           => (a -> q (m b)) -> m a -> q (m b)

Such signature didn't allow to use this function when traversed container type and type of returned by function-argument differed. Now you can use it like e.g.

concatMapM readFile files >>= putTextLn

concatForM :: (Applicative f, Monoid m, Container (l m), Element (l m) ~ m, Traversable l) => l a -> (a -> f m) -> f m Source #

Like concatMapM, but has its arguments flipped, so can be used instead of the common fmap concat $ forM pattern.

allM :: (Container f, Monad m) => (Element f -> m Bool) -> f -> m Bool Source #

Monadic and constrained to Container version of all.

>>> allM (readMaybe >=> pure . even) ["6", "10"]
Just True
>>> allM (readMaybe >=> pure . even) ["5", "aba"]
Just False
>>> allM (readMaybe >=> pure . even) ["aba", "10"]
Nothing

anyM :: (Container f, Monad m) => (Element f -> m Bool) -> f -> m Bool Source #

Monadic and constrained to Container version of any.

>>> anyM (readMaybe >=> pure . even) ["5", "10"]
Just True
>>> anyM (readMaybe >=> pure . even) ["10", "aba"]
Just True
>>> anyM (readMaybe >=> pure . even) ["aba", "10"]
Nothing

andM :: (Container f, Element f ~ m Bool, Monad m) => f -> m Bool Source #

Monadic and constrained to Container version of and.

>>> andM [Just True, Just False]
Just False
>>> andM [Just True]
Just True
>>> andM [Just True, Just False, Nothing]
Just False
>>> andM [Just True, Nothing]
Nothing
>>> andM [putTextLn "1" >> pure True, putTextLn "2" >> pure False, putTextLn "3" >> pure True]
1
2
False

orM :: (Container f, Element f ~ m Bool, Monad m) => f -> m Bool Source #

Monadic and constrained to Container version of or.

>>> orM [Just True, Just False]
Just True
>>> orM [Just True, Nothing]
Just True
>>> orM [Nothing, Just True]
Nothing