fptools.either¶
- class pythonic_fp.fptools.either.Either(value: L)¶
- class pythonic_fp.fptools.either.Either(value: L, side: EitherBool)
- class pythonic_fp.fptools.either.Either(value: R, side: EitherBool)
Either Monad
Data structure semantically containing either a left or a right value, but not both.
Implements a left biased Either Monad.
Either(value: +L, LEFT)produces a leftEitherEither(value: +L, RIGHT)produces a rightEither
In a Boolean context
A left
Eitheris “truthy”A right
Eitheris “falsy”
Two
Eitherobjects compare as equal whenboth are left values or both are right values whose values
are the same object
compare as equal
Immutable, an
Eitherdoes not change after being created. Thereforemap&bindreturn new instances.Warning
The contained value need not be immutable, therefore not hashable if value is mutable.
Note
Either(value: +L, side: Left): Either[L, R] -> left: Either[L, R]Either(value: +R, side: Right): Either[L, R] -> right: Either[L, R]- __init__(value: L) None¶
- __init__(value: L, side: EitherBool) None
- __init__(value: R, side: EitherBool) None
- __hash__() int¶
Return hash(self).
- __repr__() str¶
Return repr(self).
- __str__() str¶
Return str(self).
- __len__() int¶
An Either always contains just one value.
- __eq__(other: object) bool¶
Return self==value.
- get() L¶
Get value if a left.
Warning
Unsafe method
get. Will raiseValueErrorifEitheris a right. Best practice is to first check theEitherin a boolean context.- Returns:
its value if a Left
- Raises:
ValueError – if not a left
- map_right(f: Callable[[R], V]) Either[L, V]¶
Construct new Either with a different right.
- Parameters:
f – function to map a right value
- Returns:
a new Either if a right, otherwise itself
- map(f: Callable[[L], U]) Either[U, R]¶
Map over if a left value. Return new instance.
- Parameters:
f – function to map a left value
- Returns:
a new Either if a left, otherwise itself
- bind(f: Callable[[L], Either[U, R]]) Either[U, R]¶
Flatmap over the left value, propagate right values.
- Parameters:
f – function to flatmap a left value
- Returns:
a new Either if a left, otherwise itself
- map_except(f: Callable[[L], U], fallback_right: R) Either[U, R]¶
Map over if a left value - with fallback upon exception.
if
Eitheris a left then mapfover its valueif
freturns normally, then return a leftEither[U, R]if
fraises an exception, return rightEither[U, R]
if
Eitheris a rightreturn new
Either(right=self._right): Either[+U, +R]
Warning
Swallows exceptions.
- bind_except(f: Callable[[L], Either[U, R]], fallback_right: R) Either[U, R]¶
Flatmap
Eitherwith functionfwith a fallback right if exception is thrown.Warning
Swallows exceptions.
- Parameters:
fallback_right – fallback value if exception thrown
- Returns:
a successfully mapped left, a propagated right, or a right with fallback value
- static sequence(sequence_xor_uv: Sequence[Either]) Either[Sequence, V]¶
Sequence a sequence subtype of Sequence[Either[U, V]]``
If the iterated
Eithervalues are all lefts, then return anEitherof an iterable of the left values. Otherwise return a rightEithercontaining the first right encountered.
- class pythonic_fp.fptools.either.EitherBool¶
- class pythonic_fp.fptools.either.EitherBool(witness: object)
Boolean-like type used by Either constructor.
A “truthy” value passed to constructor produces the unique
LEFTvalue.A “falsy” value passed to constructor produces the unique
RIGHTvalue.
- Parameters:
witness – Determines truthiness of the
SBool.- Returns:
The truthy or falsy SBool class instance.
- __repr__() str¶
Return repr(self).
- pythonic_fp.fptools.either.LEFT = TRUTH¶
Singleton value signaling
Eitherconstructor to produce aLEFTEither, the default.
- pythonic_fp.fptools.either.RIGHT = LIE¶
Singleton value signaling
Eitherconstructor to produce aRIGHTEither.