Safe Haskell | Safe-Inferred |
---|
Control flow and monadic utilities.
Synopsis
- whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
- whenJustM :: Monad.Monad m => m (Maybe a) -> (a -> m ()) -> m ()
- whenM :: Monad.Monad m => m Bool -> m () -> m ()
- unlessM :: Monad.Monad m => m Bool -> m () -> m ()
- ifM :: Monad.Monad m => m Bool -> m a -> m a -> m a
- uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
- uncurry4 :: (a -> b -> c -> d -> e) -> (a, b, c, d) -> e
- while :: Monad.Monad m => m Bool -> m a -> m [a]
- while_ :: Monad.Monad m => m Bool -> m a -> m ()
- loop0 :: (a -> a) -> a
- loop1 :: forall state a. state -> ((state -> a) -> state -> a) -> a
- loop2 :: forall s1 s2 a. s1 -> s2 -> ((s1 -> s2 -> a) -> s1 -> s2 -> a) -> a
- mconcatMap :: Monoid b => (a -> b) -> [a] -> b
- concatMapM :: (Monad.Monad m, Monoid b) => (a -> m b) -> [a] -> m b
- justm :: Monad.Monad m => m (Maybe a) -> (a -> m (Maybe b)) -> m (Maybe b)
- rightm :: Monad.Monad m => m (Either err a) -> (a -> m (Either err b)) -> m (Either err b)
- firstJust :: Monad.Monad m => m (Maybe a) -> m (Maybe a) -> m (Maybe a)
- firstJusts :: Monad.Monad m => [m (Maybe a)] -> m (Maybe a)
- justErr :: err -> Maybe a -> Either err a
- tryJust :: MonadError e m => e -> Maybe a -> m a
- tryRight :: MonadError e m => Either e a -> m a
- rethrow :: MonadError e m => (e -> e) -> m a -> m a
- class Bifunctor (p :: Type -> Type -> Type) where
- findM :: Monad.Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
- andM :: Monad.Monad m => [m Bool] -> m Bool
- orM :: Monad.Monad m => [m Bool] -> m Bool
- allM :: Monad.Monad m => (a -> m Bool) -> [a] -> m Bool
- anyM :: Monad.Monad m => (a -> m Bool) -> [a] -> m Bool
- notM :: Functor m => m Bool -> m Bool
- mapMaybeM :: Monad.Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
- partitionM :: Monad.Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
- errorStack :: Stack => Text -> a
- errorIO :: Stack => MonadIO m => Text -> m a
Documentation
whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m () Source #
whenJustM :: Monad.Monad m => m (Maybe a) -> (a -> m ()) -> m () Source #
whenM :: Monad.Monad m => m Bool -> m () -> m () Source #
unlessM :: Monad.Monad m => m Bool -> m () -> m () Source #
ifM :: Monad.Monad m => m Bool -> m a -> m a -> m a Source #
while :: Monad.Monad m => m Bool -> m a -> m [a] Source #
while_ :: Monad.Monad m => m Bool -> m a -> m () Source #
loop0 :: (a -> a) -> a Source #
Loop with no arguments. This is the same as Fix.fix
but the name is
clearer.
loop1 :: forall state a. state -> ((state -> a) -> state -> a) -> a Source #
Loop with a single state argument.
loop2 :: forall s1 s2 a. s1 -> s2 -> ((s1 -> s2 -> a) -> s1 -> s2 -> a) -> a Source #
Loop with two state arguments. You could use loop1 with a pair, but sometimes the currying is convenient.
mconcatMap :: Monoid b => (a -> b) -> [a] -> b Source #
This is foldMap
specialized to lists.
concatMapM :: (Monad.Monad m, Monoid b) => (a -> m b) -> [a] -> m b Source #
This is actually a mconcatMapM.
A further generalized version would be:
foldMapA :: (Applicative f, Traversable t, Monoid m) => (a -> f m) -> t a -> f m foldMapA f = fmap Foldable.fold . traverse f
justm :: Monad.Monad m => m (Maybe a) -> (a -> m (Maybe b)) -> m (Maybe b) Source #
Run the second action only if the first action returns Just.
This is like MaybeT, but using MaybeT itself required lots of annoying explicit lifting.
rightm :: Monad.Monad m => m (Either err a) -> (a -> m (Either err b)) -> m (Either err b) Source #
The Either equivalent of justm
. EitherT solves the same problem, but
requires a runEitherT and lots of hoistEithers.
firstJust :: Monad.Monad m => m (Maybe a) -> m (Maybe a) -> m (Maybe a) Source #
Return the first action to return Just.
firstJusts :: Monad.Monad m => [m (Maybe a)] -> m (Maybe a) Source #
firstJust
applied to a list.
tryJust :: MonadError e m => e -> Maybe a -> m a Source #
I usually call this require
.
tryRight :: MonadError e m => Either e a -> m a Source #
I usually call this require_right
.
rethrow :: MonadError e m => (e -> e) -> m a -> m a Source #
class Bifunctor (p :: Type -> Type -> Type) where #
A bifunctor is a type constructor that takes
two type arguments and is a functor in both arguments. That
is, unlike with Functor
, a type constructor such as Either
does not need to be partially applied for a Bifunctor
instance, and the methods in this class permit mapping
functions over the Left
value or the Right
value,
or both at the same time.
Formally, the class Bifunctor
represents a bifunctor
from Hask
-> Hask
.
Intuitively it is a bifunctor where both the first and second arguments are covariant.
You can define a Bifunctor
by either defining bimap
or by
defining both first
and second
.
If you supply bimap
, you should ensure that:
bimap
id
id
≡id
If you supply first
and second
, ensure:
first
id
≡id
second
id
≡id
If you supply both, you should also ensure:
bimap
f g ≡first
f.
second
g
These ensure by parametricity:
bimap
(f.
g) (h.
i) ≡bimap
f h.
bimap
g ifirst
(f.
g) ≡first
f.
first
gsecond
(f.
g) ≡second
f.
second
g
Since: base-4.8.0.0
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d #
Map over both arguments at the same time.
bimap
f g ≡first
f.
second
g
Examples
>>>
bimap toUpper (+1) ('j', 3)
('J',4)
>>>
bimap toUpper (+1) (Left 'j')
Left 'J'
>>>
bimap toUpper (+1) (Right 3)
Right 4
Instances
Bifunctor Either | Since: base-4.8.0.0 |
Bifunctor Arg | Since: base-4.9.0.0 |
Bifunctor Elt Source # | |
Bifunctor Paired Source # | |
Bifunctor Either | |
Bifunctor These | |
Bifunctor Pair | |
Bifunctor Of | |
Bifunctor These | |
Bifunctor (,) | Since: base-4.8.0.0 |
Bifunctor (Const :: Type -> Type -> Type) | Since: base-4.8.0.0 |
Bifunctor (Tagged :: Type -> Type -> Type) | |
Bifunctor ((,,) x1) | Since: base-4.8.0.0 |
Bifunctor (K1 i :: Type -> Type -> Type) | Since: base-4.9.0.0 |
Bifunctor ((,,,) x1 x2) | Since: base-4.8.0.0 |
Bifunctor p => Bifunctor (Flip p) | |
Bifunctor ((,,,,) x1 x2 x3) | Since: base-4.8.0.0 |
(Bifunctor f, Bifunctor g) => Bifunctor (Product f g) | |
(Bifunctor p, Bifunctor q) => Bifunctor (Sum p q) | |
Bifunctor ((,,,,,) x1 x2 x3 x4) | Since: base-4.8.0.0 |
(Functor f, Bifunctor p) => Bifunctor (Tannen f p) | |
Bifunctor ((,,,,,,) x1 x2 x3 x4 x5) | Since: base-4.8.0.0 |
(Bifunctor p, Functor f, Functor g) => Bifunctor (Biff p f g) | |
findM :: Monad.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")
andM :: Monad.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)
orM :: Monad.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)
allM :: Monad.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)
anyM :: Monad.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)
mapMaybeM :: Monad.Monad m => (a -> m (Maybe b)) -> [a] -> m [b] #
A version of mapMaybe
that works with a monadic predicate.
partitionM :: Monad.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