extra-1.4.11: Extra functions I use.

Safe HaskellSafe
LanguageHaskell2010

Data.Either.Extra

Description

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

Documentation

isLeft :: Either a b -> Bool #

Return True if the given value is a Left-value, False otherwise.

Examples

Basic usage:

>>> isLeft (Left "foo")
True
>>> isLeft (Right 3)
False

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)
>>> report (Left "parse error")
ERROR

Since: 4.7.0.0

isRight :: Either a b -> Bool #

Return True if the given value is a Right-value, False otherwise.

Examples

Basic usage:

>>> isRight (Left "foo")
False
>>> isRight (Right 3)
True

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")
>>> report (Right 1)
SUCCESS

Since: 4.7.0.0

fromLeft :: Either l r -> l Source #

The fromLeft function extracts the element out of a Left and throws an error if its argument is Right. Much like fromJust, using this function in polished code is usually a bad idea.

\x -> fromLeft (Left  x) == x
\x -> fromLeft (Right x) == undefined

fromRight :: 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

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