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 left Either

  • Either(value: +L, RIGHT) produces a right Either

In a Boolean context

  • A left Either is “truthy”

  • A right Either is “falsy”

Two Either objects compare as equal when

  • both are left values or both are right values whose values

  • are the same object

  • compare as equal

Immutable, an Either does not change after being created. Therefore map & bind return 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 raise ValueError if Either is a right. Best practice is to first check the Either in a boolean context.

Returns:

its value if a Left

Raises:

ValueError – if not a left

get_left() MayBe

Get value of Either if a left. Safer version of get method.

Returns:

MayBe[L]

get_right() MayBe

Get value of Either if a right.

Returns:

MayBe[R]

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 Either is a left then map f over its value

    • if f returns normally, then return a left Either[U, R]

    • if f raises an exception, return right Either[U, R]

  • if Either is a right

    • return 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 Either with function f with 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 Either values are all lefts, then return an Either of an iterable of the left values. Otherwise return a right Either containing 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 LEFT value.

  • A “falsy” value passed to constructor produces the unique RIGHT value.

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 Either constructor to produce a LEFT Either, the default.

pythonic_fp.fptools.either.RIGHT = LIE

Singleton value signaling Either constructor to produce a RIGHT Either.