pfun.maybe.Maybe
Type-alias for Union[Nothing, Just[TypeVar('A')]]
pfun.maybe.Just
dataclass
Represents the result of a successful computation
get: +A
dataclass-field
The result of the computation
__eq__(self, other)
special
Test if other is a Just
Args; other: Value to compare with
Returns:
Type | Description |
---|---|
bool |
True if other is a |
and_then(self, f)
Chain together functional calls, carrying along the state of the computation that may fail.
Examples:
1 2 3 4 5 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[+A], Maybe[B]] |
the function to call |
required |
Returns:
Type | Description |
---|---|
Maybe[B] |
|
map(self, f)
Map the result of a possibly failed computation
Examples:
1 2 3 4 5 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[+A], ~B] |
Function to apply to the result |
required |
Returns:
Type | Description |
---|---|
Maybe[B] |
|
or_else(self, default)
Try to get the result of the possibly failed computation if it was successful.
Examples:
1 2 3 4 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
~B |
Value to return if computation has failed |
required |
Returns:
Type | Description |
---|---|
Union[+A, ~B] |
|
pfun.maybe.Nothing
dataclass
Represents a failed computation
__eq__(self, other)
special
Test if other is a Nothing
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Any |
Value to compare with |
required |
Returns:
Type | Description |
---|---|
bool |
True if other is a |
and_then(self, f)
Chain together functional calls, carrying along the state of the computation that may fail.
Examples:
1 2 3 4 5 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[+A], Maybe[B]] |
the function to call |
required |
Returns:
Type | Description |
---|---|
Maybe[B] |
|
map(self, f)
Map the result of a possibly failed computation
Examples:
1 2 3 4 5 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[Any], ~B] |
Function to apply to the result |
required |
Returns:
Type | Description |
---|---|
Maybe[B] |
|
or_else(self, default)
Try to get the result of the possibly failed computation if it was successful.
Examples:
1 2 3 4 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
~B |
Value to return if computation has failed |
required |
Returns:
Type | Description |
---|---|
Union[+A, ~B] |
|
pfun.maybe.maybe(f)
Wrap a function that may raise an exception with a Maybe
.
Can also be used as a decorator. Useful for turning
any function into a monadic function
Examples:
1 2 3 4 5 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[..., ~B] |
Function to wrap |
required |
Returns:
Type | Description |
---|---|
Callable[..., Union[pfun.maybe.Nothing, pfun.maybe.Just[~B]]] |
f wrapped with a |
pfun.maybe.flatten(maybes)
Extract value from each Maybe
, ignoring
elements that are Nothing
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
maybes |
Sequence[Union[pfun.maybe.Nothing, pfun.maybe.Just[+A]]] |
Seqence of |
required |
Returns:
Type | Description |
---|---|
pfun.list.List[+A] |
|
pfun.maybe.for_each(f, iterable)
Map each in element in iterable
to
an Maybe
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], Union[pfun.maybe.Nothing, pfun.maybe.Just[~B]]] |
Function to map over |
required |
iterable |
Iterable[+A] |
Iterable to map |
required |
Returns:
Type | Description |
---|---|
Union[pfun.maybe.Nothing, pfun.maybe.Just[Iterable[~B]]] |
|
pfun.maybe.sequence(iterable)
Evaluate each Maybe
in iterable
from left to right
and collect the results
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable[Union[pfun.maybe.Nothing, pfun.maybe.Just[+A]]] |
The iterable to collect results from |
required |
Returns:
Type | Description |
---|---|
Union[pfun.maybe.Nothing, pfun.maybe.Just[Iterable[+A]]] |
|
pfun.maybe.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], Union[pfun.maybe.Nothing, pfun.maybe.Just[bool]]] |
Function to map |
required |
iterable |
Iterable[+A] |
Iterable to map by |
required |
Returns:
Type | Description |
---|---|
Union[pfun.maybe.Nothing, pfun.maybe.Just[Iterable[+A]]] |
|
pfun.maybe.from_optional(optional)
Return a possible None value to Maybe
Examples:
1 2 3 4 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optional |
Optional[+A] |
optional value to convert to |
required |
Returns:
Type | Description |
---|---|
Union[pfun.maybe.Nothing, pfun.maybe.Just[+A]] |
|