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) special

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

and_then(self, f)

Chain together functional calls, carrying along the state of the computation that may fail.

Examples:

1
2
3
4
5
>>> f = lambda i: Just(1 / i) if i != 0 else Nothing()
>>> Just(2).and_then(f)
Just(0.5)
>>> Just(0).and_then(f)
Nothing()

Parameters:

Name Type Description Default
f Callable[[+A], Maybe[B]]

the function to call

required

Returns:

Type Description
Maybe[B]

Just wrapping a value of type A if the computation was successful, Nothing otherwise.

map(self, f)

Map the result of a possibly failed computation

Examples:

1
2
3
4
5
>>> f = lambda i: Just(1 / i) if i != 0 else Nothing()
>>> Just(2).and_then(f).map(str)
Just('0.5')
>>> Just(0).and_then(f).map(str)
Nothing()

Parameters:

Name Type Description Default
f Callable[[+A], ~B]

Function to apply to the result

required

Returns:

Type Description
Maybe[B]

Just wrapping result of type B if the computation was

or_else(self, default)

Try to get the result of the possibly failed computation if it was successful.

Examples:

1
2
3
4
>>> Just(1).or_else(2)
1
>>> Nothing().or_else(2)
2

Parameters:

Name Type Description Default
default ~B

Value to return if computation has failed

required

Returns:

Type Description
Union[+A, ~B]

value if this is Just(value), default_value if this Nothing

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

and_then(self, f)

Chain together functional calls, carrying along the state of the computation that may fail.

Examples:

1
2
3
4
5
>>> f = lambda i: Just(1 / i) if i != 0 else Nothing()
>>> Just(2).and_then(f)
Just(0.5)
>>> Just(0).and_then(f)
Nothing()

Parameters:

Name Type Description Default
f Callable[[+A], Maybe[B]]

the function to call

required

Returns:

Type Description
Maybe[B]

Just wrapping a value of type A if the computation was successful, Nothing otherwise.

map(self, f)

Map the result of a possibly failed computation

Examples:

1
2
3
4
5
>>> f = lambda i: Just(1 / i) if i != 0 else Nothing()
>>> Just(2).and_then(f).map(str)
Just('0.5')
>>> Just(0).and_then(f).map(str)
Nothing()

Parameters:

Name Type Description Default
f Callable[[Any], ~B]

Function to apply to the result

required

Returns:

Type Description
Maybe[B]

Just wrapping result of type B if the computation was

or_else(self, default)

Try to get the result of the possibly failed computation if it was successful.

Examples:

1
2
3
4
>>> Just(1).or_else(2)
1
>>> Nothing().or_else(2)
2

Parameters:

Name Type Description Default
default ~B

Value to return if computation has failed

required

Returns:

Type Description
Union[+A, ~B]

value if this is Just(value), default_value if this Nothing

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
>>> 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:

1
2
>>> 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:

1
2
>>> 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.sequence(iterable)

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

Examples:

1
2
>>> sequence([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:

1
2
>>> 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:

1
2
3
4
>>> 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