Skip to content

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)

Test if other is a Just

Args; other: Value to compare with

Returns:

Type Description
bool

True if other is a Just and its wrapped value equals the wrapped value of this instance

map(self, f)

Map function f over the value wrapped by this functor

:param f: The function to apply to the value wrapped by this Functor :return: The result of applying f to the wrapped value

pfun.maybe.Nothing dataclass

Represents a failed computation

__eq__(self, other)

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 Nothing, False otherwise

map(self, f)

Map function f over the value wrapped by this functor

:param f: The function to apply to the value wrapped by this Functor :return: The result of applying f to the wrapped value

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:

>>> to_int = maybe(int)
>>> to_int("1")
Just(1)
>>> to_int("Whoops")
Nothing()

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 Maybe

pfun.maybe.flatten(maybes)

Extract value from each Maybe, ignoring elements that are Nothing

Examples:

>>> flatten([Just(1), Nothing(), Just(2)])
List((1, 2))

Parameters:

Name Type Description Default
maybes Sequence[Union[pfun.maybe.Nothing, pfun.maybe.Just[+A]]]

Seqence of Maybe

required

Returns:

Type Description
pfun.list.List[+A]

List of unwrapped values

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:

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

Parameters:

Name Type Description Default
f Callable[[+A], Union[pfun.maybe.Nothing, pfun.maybe.Just[~B]]]

Function to map over iterable

required
iterable Iterable[+A]

Iterable to map f over

required

Returns:

Type Description
Union[pfun.maybe.Nothing, pfun.maybe.Just[Iterable[~B]]]

f mapped over iterable and combined from left to right.

pfun.maybe.gather(iterable)

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

Examples:

>>> gather([Just(v) for v in range(3)])
Just((0, 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]]]

Maybe of collected results

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:

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

Parameters:

Name Type Description Default
f Callable[[+A], Union[pfun.maybe.Nothing, pfun.maybe.Just[bool]]]

Function to map iterable by

required
iterable Iterable[+A]

Iterable to map by f

required

Returns:

Type Description
Union[pfun.maybe.Nothing, pfun.maybe.Just[Iterable[+A]]]

iterable mapped and filtered by f

pfun.maybe.from_optional(optional)

Return a possible None value to Maybe

Examples:

>>> from_optional('value')
Just('value')
>>> from_optional(None)
Nothing()

Parameters:

Name Type Description Default
optional Optional[+A]

optional value to convert to Maybe

required

Returns:

Type Description
Union[pfun.maybe.Nothing, pfun.maybe.Just[+A]]

Just(optional) if optional is not None, Nothing otherwise