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

Language | Haskell2010 |

This module extends Data.Either with extra operations, particularly
to quickly extract from inside an `Either`

. Some of these operations are
partial, and should be used with care in production-quality code.

## Synopsis

- module Data.Either
- isLeft :: Either a b -> Bool
- isRight :: Either a b -> Bool
- fromLeft :: a -> Either a b -> a
- fromRight :: b -> Either a b -> b
- fromEither :: Either a a -> a
- fromLeft' :: Partial => Either l r -> l
- fromRight' :: Partial => Either l r -> r
- eitherToMaybe :: Either a b -> Maybe b
- maybeToEither :: a -> Maybe b -> Either a b
- mapLeft :: (a -> c) -> Either a b -> Either c b
- mapRight :: (b -> c) -> Either a b -> Either a c

# Documentation

module Data.Either

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*

fromLeft :: a -> Either a b -> a #

Return the contents of a `Left`

-value or a default value otherwise.

#### Examples

Basic usage:

`>>>`

3`fromLeft 1 (Left 3)`

`>>>`

1`fromLeft 1 (Right "foo")`

*Since: base-4.10.0.0*

fromRight :: b -> Either a b -> b #

Return the contents of a `Right`

-value or a default value otherwise.

#### Examples

Basic usage:

`>>>`

3`fromRight 1 (Right 3)`

`>>>`

1`fromRight 1 (Left "foo")`

*Since: base-4.10.0.0*

fromEither :: Either a a -> a Source #

Pull the value out of an `Either`

where both alternatives
have the same type.

\x -> fromEither (Left x ) == x \x -> fromEither (Right x) == x

fromRight' :: Partial => Either l r -> r Source #

The `fromRight'`

function extracts the element out of a `Right`

and
throws an error if its argument is `Left`

.
Much like `fromJust`

, using this function in polished code is usually a bad idea.

\x -> fromRight' (Right x) == x \x -> fromRight' (Left x) == undefined

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

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