Left package:relude

Maps left part of Either to Maybe.
>>> leftToMaybe (Left True)
Just True

>>> leftToMaybe (Right "aba")
Nothing
Extracts from a list of Either all the Left elements. All the Left elements are extracted in order.

Examples

Basic usage:
>>> let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]

>>> lefts list
["foo","bar","baz"]
Return the contents of a Left-value or a default value otherwise. @since base-4.10.0.0

Examples

Basic usage:
>>> fromLeft 1 (Left 3)
3

>>> fromLeft 1 (Right "foo")
1
Maps Maybe to Either wrapping default value into Right.
>>> maybeToLeft True (Just "aba")
Left "aba"

>>> maybeToLeft True Nothing
Right True
Applies given action to Either content if Left is given and returns the result. In case of Right the default value will be returned.
>>> whenLeft "bar" (Left 42) (\a -> "success!" <$ print a)
42
"success!"
>>> whenLeft "bar" (Right 42) (\a -> "success!" <$ print a)
"bar"
Monadic version of whenLeft.
>>> whenLeftM "bar" (pure $ Left 42) (\a -> "success!" <$ print a)
42
"success!"
>>> whenLeftM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)
"bar"
Monadic version of whenLeft_.
>>> whenLeftM_ (pure $ Right 42) putTextLn

>>> whenLeftM_ (pure $ Left "foo") putTextLn
foo
Applies given action to Either content if Left is given.
>>> whenLeft_ (Right 42) putTextLn

>>> whenLeft_ (Left "foo") putTextLn
foo
Return True if the given value is a Left-value, False otherwise. @since base-4.7.0.0

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