Skip to content

pfun.either.Either

Type-alias for Union[Left[TypeVar('L')], Right[TypeVar('R')]]

pfun.either.Left dataclass

Represents the Left case of Either

get: +B dataclass-field

The left result

__eq__(self, other) special

Test if other is an Left wrapping the same value as this instance

Examples:

1
2
3
4
>>> Left('error message') == Left('error message')
True
>>> Left('error message') == Left('another message')
False

Parameters:

Name Type Description Default
other object

object to compare with

required

Returns:

Type Description
bool

True if other is an Left instance and wraps the same value as this instance, False otherwise

and_then(self, f)

Chain together functions of either computations, keeping track of whether or not any of them have failed

Examples:

1
2
3
4
5
>>> f = lambda i: Right(1 / i) if i != 0 else Left('i was 0')
>>> Right(1).and_then(f)
Right(1.0)
>>> Right(0).and_then(f)
Left('i was 0')

Parameters:

Name Type Description Default
f Callable[[A], Either[B, C]]

The function to call

required

Returns:

Type Description
Either[B, C]

Right of type A if the computation was successful, Left of type B otherwise.

map(self, f)

Map the result of this either computation

Examples:

1
2
3
4
5
6
>>> f = (lambda i: Right(1 / i)
...                if i != 0 else Left('i was 0').map(str))
>>> Right(1).and_then(f).map(str)
Right('0.5')
>>> Ok(0).and_then(f).map(str)
Left('i was 0')

Parameters:

Name Type Description Default
f Callable[[A], C]

Function to apply to the result

required

Returns:

Type Description
Either[B, C]

Right wrapping result of type C if the computation was if this is a Right value, Left of type B otherwise

or_else(self, default)

Try to get the result of this either computation, return default if this is a Left value

Examples:

1
2
3
4
>>> Right(1).or_else(2)
1
>>> Left(1).or_else(2)
2

Parameters:

Name Type Description Default
default C

Value to return if this is a Left value

required

Returns:

Type Description
C

Result of computation if this is a Right value, default otherwise

pfun.either.Right dataclass

Represents the Right case of Either

get: +A dataclass-field

The right result

__eq__(self, other) special

Test if other is a Right wrapping the same value as this instance

Examples:

1
2
3
4
>>> Right('value') == Right('value')
True
>>> Right('another value') == Right('value')
False

Parameters:

Name Type Description Default
other Any

object to compare with

required

Returns:

Type Description
bool

True if other is a Right instance and wraps the same value as this instance, False otherwise

and_then(self, f)

Chain together functions of either computations, keeping track of whether or not any of them have failed

Examples:

1
2
3
4
5
>>> f = lambda i: Right(1 / i) if i != 0 else Left('i was 0')
>>> Right(1).and_then(f)
Right(1.0)
>>> Right(0).and_then(f)
Left('i was 0')

Parameters:

Name Type Description Default
f Callable[[A], Either[B, C]]

The function to call

required

Returns:

Type Description
Either[B, C]

Right of type A if the computation was successful, Left of type B otherwise.

map(self, f)

Map the result of this either computation

Examples:

1
2
3
4
5
6
>>> f = (lambda i: Right(1 / i)
...                if i != 0 else Left('i was 0').map(str))
>>> Right(1).and_then(f).map(str)
Right('0.5')
>>> Ok(0).and_then(f).map(str)
Left('i was 0')

Parameters:

Name Type Description Default
f Callable[[A], C]

Function to apply to the result

required

Returns:

Type Description
Either[Any, C]

Right wrapping result of type C if the computation was if this is a Right value, Left of type B otherwise

or_else(self, default)

Try to get the result of this either computation, return default if this is a Left value

Examples:

1
2
3
4
>>> Right(1).or_else(2)
1
>>> Left(1).or_else(2)
2

Parameters:

Name Type Description Default
default C

Value to return if this is a Left value

required

Returns:

Type Description
A

Result of computation if this is a Right value, default otherwise

pfun.either.either(f)

Turn f into a monadic function in the Either monad by wrapping in it a Right

Examples:

1
2
>>> either(lambda v: v)(1)
Right(1)

Parameters:

Name Type Description Default
f Callable[..., A]

function to wrap

required

Returns:

Type Description
Callable[..., Either[A, B]]

f wrapped with a Right

pfun.either.for_each(f, iterable)

Map each in element in iterable to an Either by applying f, combine the elements by and_then from left to right and collect the results

Examples:

1
2
>>> for_each(Right, range(3))
Right((0, 1, 2))

Parameters:

Name Type Description Default
f Callable[[A], Either[B, C]]

Function to map over iterable

required
iterable Iterable[A]

Iterable to map f over

required

Returns:

Type Description
Either[B, Iterable[C]]

f mapped over iterable and combined from left to right.

pfun.either.sequence(iterable)

Evaluate each Either in iterable from left to right and collect the results

Examples:

1
2
>>> sequence([Right(v) for v in range(3)])
Right((0, 1, 2))

Parameters:

Name Type Description Default
iterable Iterable[Either[A, B]]

The iterable to collect results from

required

Returns:

Type Description
Either[A, Iterable[B]]

Either of collected results

pfun.either.filter_(f, iterable)

Map each element in iterable by applying f, filter the results by the value returned by f and combine from left to right.

Examples:

1
2
>>> filter(lambda v: Right(v % 2 == 0), range(3))
Right((0, 2))

Parameters:

Name Type Description Default
f Callable[[A], Either[B, bool]]

Function to map iterable by

required
iterable Iterable[A]

Iterable to map by f

required

Returns:

Type Description
Either[B, Iterable[A]]

iterable mapped and filtered by f

pfun.either.catch(f)

Decorator that wraps return values of decoratod functions with Right, and wraps catched exceptions with Left

Examples:

1
2
3
4
5
>>> catch_division = catch(lambda v: 1 / v)
>>> catch_division(1)
Right(1.0)
>>> catch_division(0)
Left(ZeroDivisionError('division by zero'))

Parameters:

Name Type Description Default
f Callable[..., A]

function to decorate

required

Returns:

Type Description
Callable[..., Either[Exception, A]]

decorated function