Right package:rebase

A mirror image of left. The default definition may be overridden with a more efficient version if desired.
Laws:
right'dimap swapE swapE . left' where
swapE :: Either a b -> Either b a
swapE = either Right Left
rmap Rightlmap Right . right'
lmap (left f) . right'rmap (left f) . right'
right' . right'dimap unassocE assocE . right' where
assocE :: Either (Either a b) c -> Either a (Either b c)
assocE (Left (Left a)) = Left a
assocE (Left (Right b)) = Right (Left b)
assocE (Right c) = Right (Right c)
unassocE :: Either a (Either b c) -> Either (Either a b) c
unassocE (Left a) = Left (Left a)
unassocE (Right (Left b)) = Left (Right b)
unassocE (Right (Right c)) = Right c
Maybe get the Right side of an Either.
rightToMaybeeither (const Nothing) Just
Using Control.Lens:
rightToMaybe ≡ preview _Right
rightToMaybe x ≡ x^?_Right
>>> rightToMaybe (Left 12)
Nothing
>>> rightToMaybe (Right 12)
Just 12
Extracts from a list of Either all the Right elements. All the Right elements are extracted in order.

Examples

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

>>> rights list
[3,7]
A more meaningful and conflict-free alias for second.
Return the contents of a Right-value or a default value otherwise.

Examples

Basic usage:
>>> fromRight 1 (Right 3)
3

>>> fromRight 1 (Left "foo")
1
Extracts the element out of a Right and throws an error if its argument take the form Left _. Using Control.Lens:
fromRight' x ≡ x^?!_Right
>>> fromRight' (Right 12)
12
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
Maybe produce a Right, otherwise produce a Left.
>>> maybeToRight "default" (Just 12)
Right 12
>>> maybeToRight "default" Nothing
Left "default"
A synonym of whenLeft.
Laws:
unrightunleft . dimap swapE swapE where
swapE :: Either a b -> Either b a
swapE = either Right Left
rmap (either absurd id) ≡ unright . lmap (either absurd id)
unsecond . rmap (first f) ≡ unsecond . lmap (first f)
unright . unrightunright . dimap unassocE assocE where
assocE :: Either (Either a b) c -> Either a (Either b c)
assocE (Left (Left a)) = Left a
assocE (Left (Right b)) = Right (Left b)
assocE (Right c) = Right (Right c)
unassocE :: Either a (Either b c) -> Either (Either a b) c
unassocE (Left a) = Left (Left a)
unassocE (Right (Left b)) = Left (Right b)
unassocE (Right (Right c)) = Right c
Keep running an effectful computation until it returns a Right value, collecting the Left's using a supplied Monoid instance.
The whenRight function takes an Either value and a function which returns a monad. The monad is only executed when the given argument takes the form Right _, otherwise it does nothing. Using Data.Foldable:
whenRightforM_
Using Control.Lens:
whenRight ≡ forOf_ _Right
>>> whenRight (Right 12) print
12