either monad¶
The Either monad
Data structure semantically containing either a “left” value or a “right” value, but not both.
Module implements a left biased either monad
left value is intended for the “expected” result
right value gives information on something “exceptional”
left and right values can be the same or different types
in a boolean context
left values are truthy
right values are falsy
Tip
Happy path without exceptions.
Instead of catching an exception whenever the “happy path” fails, process the left values and either deal with or propagate right values.
Tip
Right Either instances can be used as sentinel values.
- final class pythonic_fp.fptools.either.Either¶
Bases:
GenericEither monad
Left biased Either monad.
immutable semantics
contains either a “left” or a “right” item, but not both
hashable
Important
An
Eitheris immutable once initialized.- __init__(value: L) None¶
- __init__(value: L, side: EitherFlag) None
- __init__(value: R, side: EitherFlag) None
initializer
Initialize
Eitherinstance as a “left” or a “right”.- param value:
The value contained in the
Either.- param side:
Determines whether to produce a left or right
Either.
- __hash__() int¶
hashability
If contained value hashable, use its hash value in the hash calculation, otherwise use item’s identity.
should be safe, the
Eitherholds a reference to the valuelazily calculates hash value, then caches it
hash also depends if
Eitheris a left or right
- __bool__() bool¶
bool
left
Eitheris truthyright
Eitheris falsy
- __len__() int¶
len
Either always contains just one value.
- __eq__(other: object) bool¶
equality comparison
Compare
Eitherto another object. Compare first by identity, then value.
- __iter__() Iterator¶
iterate
Iterate
valueif a leftEither.
- __repr__() str¶
representation string
Return the strings
‘Either(repr_value, LEFT)’ if a left
‘Either(repr_value, RIGHT)’ if if a right
Where
repr_value = repr(value).
- __str__() str¶
user string
Return the strings
‘Either(str_value)’ when a left
‘Either(str_value, RIGHT)’ when a right
Where
str_value = str(value).
- get() L¶
get
Get value if a left.
- returns:
The value if a left.
- raises ValueError:
If not a left.
Warning
Unsafe method
get. Will raiseValueErrorifEitheris a right.Tip
Best practice is to first check the
Eitherin a boolean context.
- map_right(f: Callable[[R], V]) Either[L, V]¶
map right
Map function
fover the contents of a rightEither.- param f:
function to map a right value
- returns:
A new
Eitherif a right, otherwiseself.
- map(f: Callable[[L], U]) Either[U, R]¶
map
Map function
fover leftEither.- param f:
Function used to map left values.
- returns:
A new
Eitherif a left, otherwiseself.
- map_except(f: Callable[[L], U], fallback_right: R) Either[U, R]¶
map except
Map
fover leftEitherwith a right fallback upon exception.- param f:
Function used to map left values.
- param fallback_right:
Fallback value if exception thrown.
- returns:
A successfully mapped left, a propagated right, or a right with a fallback value.
Warning
Swallows exceptions.
- bind(f: Callable[[L], Either[U, R]]) Either[U, R]¶
bind
Flatmap function
fover a left value. Propagate right values.- param f:
Function to bind.
- returns:
A new Either if a left, otherwise itself.
- bind_except(f: Callable[[L], Either[U, R]], fallback_right: R) Either[U, R]¶
bind except
Flatmap function
foverEither, with fallback upon exception. Propagate right values.- param f:
Function to bind over values.
- param fallback_right:
Fallback value if exception thrown.
- returns:
A successfully bound left, a propagated right, or a right with a fallback value.
Warning
Swallows exceptions.
- final class pythonic_fp.fptools.either.EitherFlag¶
Bases:
SBoolType for
LEFTandRIGHTsingleton flagsBoolean-like type for signaling the
Eitherinitializer to make a left or rightEitherinstance.- __repr__() str¶
string representation
Two values ‘LEFT’ or ‘RIGHT’ for the truthy and falsy singletons respectfully. Also the default user strings.
- pythonic_fp.fptools.either.LEFT: Final[EitherFlag] = TRUTH¶
truthy Either flag
Used by
Eitherinitializer to make a rightEither.
- pythonic_fp.fptools.either.RIGHT: Final[EitherFlag] = LIE¶
falsy Either flag
Used by
Eitherinitializer to make a rightEither.