Left package:rio

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"]
_Left targets the value contained in an Either, provided it's a Left. Gathering all Lefts in a structure (like the lefts function, but not necessarily just for lists):
>>> [Left 1, Right 'c', Left 3] ^.. each._Left
[1,3]
Checking whether an Either is a Left (like isLeft):
>>> has _Left (Left 1)
True
>>> has _Left (Right 1)
False
Extracting a value (if you're sure it's a Left):
>>> Left 1 ^?! _Left
1
Mapping over all Lefts:
>>> (each._Left %~ map toUpper) [Left "foo", Right "bar"]
[Left "FOO",Right "bar"]
Implementation:
_Left f (Left a)  = Left <$> f a
_Left _ (Right b) = pure (Right b)
Return the contents of a Left-value or a default value otherwise.

Examples

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

>>> fromLeft 1 (Right "foo")
1
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
Apply a function to a Left constructor
O(n) Left-justify a string to the given length, using the specified fill character on the right. Subject to fusion. Performs replacement on invalid scalar values. Examples:
>>> justifyLeft 7 'x' "foo"
"fooxxxx"
>>> justifyLeft 3 'x' "foobar"
"foobar"
O(n) Left-justify a string to the given length, using the specified fill character on the right. Subject to fusion. Performs replacement on invalid scalar values. Examples:
justifyLeft 7 'x' "foo"    == "fooxxxx"
justifyLeft 3 'x' "foobar" == "foobar"