maybe monad

final class pythonic_fp.fptools.maybe.MayBe

Bases: Generic

Maybe Monad

Data structure wrapping a potentially missing item.

  • immutable semantics

  • can store any item of any type, including None

  • hashable

__init__() → None
__init__(data: D) → None

init

Initialize MayBe with 1 or 0 data items.

param data:

Optional data item for the MayBe instance.

Important

  • A MayBe is immutable once initialized.

  • MayBe() is not a singleton.

__hash__() → int

hash

If contained item hashable, use its hash value in the hash calculation, otherwise use item’s identity.

  • should be safe, the MayBe holds a reference to the item.

  • Lazily calculates hash value, then caches it.

__bool__() → bool

bool

Truthy when not empty.

returns:

True if not empty, False if empty.

__len__() → int

len

Zero or one items.

__eq__(other: object) → bool

equality comparison

Compare MayBe instance to another object. Compare first by identity, then value.

returns:

True only if other object is a MayBe with a corresponding item, or both empty.

__iter__() → Iterator

iterate

yields:

The contained item if non-empty.

__repr__() → str

repr string

Return the strings

  • ‘MayBe()’ if empty

  • ‘MayBe(repr_item)’ if not empty

Where repr_item = repr(item).

returns:

A string to reproduce the MayBe.

__str__() → str

user string

Return the strings

  • ‘MayBe(str_item)’ when not empty

  • ‘MayBe()’ when empty

Where str_item = str(item).

returns:

A string meaningful to an end user.

get() → D
get(alt: D) → D

get

Return the item if it exists, otherwise an optional alternate item.

param alt:

Optional alternative item to return if MayBe empty.

returns:

The item if it exists.

raises ValueError:

When an alternate item is not provided but needed.

Warning

Unsafe method get will raise ValueError() if the MayBe is empty and an alternate return item not provided.

Tip

Best practice is to first check the MayBe in a boolean context. Threadsafe since a MayBe is immutable once created.

map(f: Callable[[D], U]) → MayBe

map

Map function f over the MayBe.

param f:

Mapping function.

returns:

A new MayBe instance if not empty, otherwise itself.

map_except(f: Callable[[D], U]) → MayBe

map_except

Map function f over the MayBe.

param f:

Mapping function.

returns:

New MayBe instance if not empty and exception is not thrown, an empty Maybe if exception is thrown, otherwise itself.

Note

Swallows exceptions of types

  • LookupError

  • ValueError

  • ArithmeticError

  • RuntimeError

Does not attempt to stop exceptions

  • TypeError

  • AttributeError

  • KeyboardInterrupt

bind(f: Callable[[D], MayBe]) → MayBe

bind

Flatmap function f over the MayBe.

param f:

Function to bind.

returns:

A new MayBe instance if not empty, otherwise itself.

bind_except(f: Callable[[D], MayBe]) → MayBe

bind_except

Flatmap function f over the MayBe.

param f:

Function to bind over contained values.

returns:

A successfully bound MayBe[U], a propagated

empty MayBe[U], or an empty MayBe[U] if an exception is raised.

Note

Swallows exceptions of types

  • LookupError

  • ValueError

  • ArithmeticError

  • RuntimeError

Does not attempt to stop exceptions

  • TypeError

  • AttributeError

  • KeyboardInterrupt

static sequence(iterable_mb_u: Iterable[MayBe]) → MayBe[Iterable]

MayBe.sequence

Iterable[MayBe[U]] -> MayBe[Iterable[U]]

param sequence_mb_u:

An Iterable of MayBe[U] values.

returns:

Empty MayBe if one of the MayBe is empty, otherwise a MayBe of an Iterable of the contained values.

Note

A sequenced empty Iterable[MayBe[U]] would produce a MayBe of an empty Iterable, not an empty MayBe.