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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
object to compare with |
required |
Returns:
Type | Description |
---|---|
bool |
True if other is an |
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Either[B, C]] |
The function to call |
required |
Returns:
Type | Description |
---|---|
Either[B, C] |
|
map(self, f)
Map the result of this either computation
Examples:
1 2 3 4 5 6 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], C] |
Function to apply to the result |
required |
Returns:
Type | Description |
---|---|
Either[B, C] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
C |
Value to return if this is a |
required |
Returns:
Type | Description |
---|---|
C |
Result of computation if this is a |
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Any |
object to compare with |
required |
Returns:
Type | Description |
---|---|
bool |
True if other is a |
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Either[B, C]] |
The function to call |
required |
Returns:
Type | Description |
---|---|
Either[B, C] |
|
map(self, f)
Map the result of this either computation
Examples:
1 2 3 4 5 6 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], C] |
Function to apply to the result |
required |
Returns:
Type | Description |
---|---|
Either[Any, C] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
C |
Value to return if this is a |
required |
Returns:
Type | Description |
---|---|
A |
Result of computation if this is a |
pfun.either.either(f)
Turn f
into a monadic function in the Either
monad by wrapping
in it a Right
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[..., A] |
function to wrap |
required |
Returns:
Type | Description |
---|---|
Callable[..., Either[A, B]] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Either[B, C]] |
Function to map over |
required |
iterable |
Iterable[A] |
Iterable to map |
required |
Returns:
Type | Description |
---|---|
Either[B, Iterable[C]] |
|
pfun.either.sequence(iterable)
Evaluate each Either
in iterable
from left to right
and collect the results
Examples:
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]] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Either[B, bool]] |
Function to map |
required |
iterable |
Iterable[A] |
Iterable to map by |
required |
Returns:
Type | Description |
---|---|
Either[B, Iterable[A]] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[..., A] |
function to decorate |
required |
Returns:
Type | Description |
---|---|
Callable[..., Either[Exception, A]] |
decorated function |