Safe Haskell | None |
---|---|

Language | Haskell2010 |

Utilities for the `Either`

type.

## Synopsis

- whileLeft :: Monad m => (a -> Either b c) -> (a -> b -> m a) -> (a -> c -> m d) -> a -> m d
- caseEitherM :: Monad m => m (Either a b) -> (a -> m c) -> (b -> m c) -> m c
- mapLeft :: (a -> c) -> Either a b -> Either c b
- mapRight :: (b -> d) -> Either a b -> Either a d
- traverseEither :: Functor f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d)
- isLeft :: Either a b -> Bool
- isRight :: Either a b -> Bool
- fromLeft :: (b -> a) -> Either a b -> a
- fromRight :: (a -> b) -> Either a b -> b
- fromLeftM :: Monad m => (b -> m a) -> m (Either a b) -> m a
- fromRightM :: Monad m => (a -> m b) -> m (Either a b) -> m b
- maybeLeft :: Either a b -> Maybe a
- maybeRight :: Either a b -> Maybe b
- allLeft :: [Either a b] -> Maybe [a]
- allRight :: [Either a b] -> Maybe [b]
- groupByEither :: forall a b. [Either a b] -> [Either [a] [b]]
- maybeToEither :: Maybe a -> Either () a

# Documentation

whileLeft :: Monad m => (a -> Either b c) -> (a -> b -> m a) -> (a -> c -> m d) -> a -> m d Source #

Loop while we have an exception.

caseEitherM :: Monad m => m (Either a b) -> (a -> m c) -> (b -> m c) -> m c Source #

Monadic version of `either`

with a different argument ordering.

traverseEither :: Functor f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d) Source #

`Either`

is bitraversable.
Note: From `base >= 4.10.0.0`

already present in `Bitraversable`

.

isLeft :: Either a b -> Bool #

Return `True`

if the given value is a `Left`

-value, `False`

otherwise.

#### Examples

Basic usage:

`>>>`

True`isLeft (Left "foo")`

`>>>`

False`isLeft (Right 3)`

Assuming a `Left`

value signifies some sort of error, we can use
`isLeft`

to write a very simple error-reporting function that does
absolutely nothing in the case of success, and outputs "ERROR" if
any error occurred.

This example shows how `isLeft`

might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:

`>>>`

`import Control.Monad ( when )`

`>>>`

`let report e = when (isLeft e) $ putStrLn "ERROR"`

`>>>`

`report (Right 1)`

`>>>`

ERROR`report (Left "parse error")`

*Since: base-4.7.0.0*

isRight :: Either a b -> Bool #

Return `True`

if the given value is a `Right`

-value, `False`

otherwise.

#### Examples

Basic usage:

`>>>`

False`isRight (Left "foo")`

`>>>`

True`isRight (Right 3)`

Assuming a `Left`

value signifies some sort of error, we can use
`isRight`

to write a very simple reporting function that only
outputs "SUCCESS" when a computation has succeeded.

This example shows how `isRight`

might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:

`>>>`

`import Control.Monad ( when )`

`>>>`

`let report e = when (isRight e) $ putStrLn "SUCCESS"`

`>>>`

`report (Left "parse error")`

`>>>`

SUCCESS`report (Right 1)`

*Since: base-4.7.0.0*

fromRightM :: Monad m => (a -> m b) -> m (Either a b) -> m b Source #

Analogue of `fromMaybeM`

.

maybeLeft :: Either a b -> Maybe a Source #

Safe projection from `Left`

.

maybeLeft (Left a) = Just a maybeLeft Right{} = Nothing

maybeRight :: Either a b -> Maybe b Source #

Safe projection from `Right`

.

maybeRight (Right b) = Just b maybeRight Left{} = Nothing

groupByEither :: forall a b. [Either a b] -> [Either [a] [b]] Source #