Maybe¶
from pymonet.Maybe import Maybe
def get_index(item):
if item in [1,2,3]:
return Maybe.just(42)
return Maybe.nothing()
get_index(42).get_or_else(0) # 0
get_index(1).get_or_else(0) # 3
get_index(42)\
.map(lambda value: value + 1)\
.bind(lambda value: Maybe.just(value + 1))\
.get_or_else(0)
# 0
get_index(1)\
.map(lambda value: value + 1)\
.bind(lambda value: Maybe.just(value + 1))\
.get_or_else(0)
get_index(42)\
.filter(lambda value: value % 2 == 0)\
.get_or_else(0)
# 0
get_index(3)\
.filter(lambda value: value % 2 == 0)\
.get_or_else(0)
# 0
get_index(2)\
.filter(lambda value: value % 2 == 0)\
.get_or_else(0)
# 2
-
class
pymonet.maybe.
Maybe
(value: T, is_nothing: bool)¶ Maybe type is the most common way of representing nothingness (or the null type). Maybe is effectively abstract and has two concrete subtypes: Box (also Some) and Nothing.
-
bind
(mapper: Callable[[T], Maybe[U]]) → Union[pymonet.maybe.Maybe[~U][U], pymonet.maybe.Maybe[NoneType][None]]¶ If Maybe is empty return new empty Maybe, in other case takes mapper function and returns result of mapper.
- Parameters
mapper (Function(A) -> Maybe[B]) – function to call with Maybe.value
- Returns
Maybe[B | None]
-
filter
(filterer: Callable[[T], bool]) → Union[pymonet.maybe.Maybe[~T][T], pymonet.maybe.Maybe[NoneType][None]]¶ If Maybe is empty or filterer returns False return default_value, in other case return new instance of Maybe with the same value.
- Parameters
filterer (Function(A) -> Boolean) –
- Returns
copy of self when filterer returns True, in other case empty Maybe
- Return type
Maybe[A] | Maybe[None]
-
get_or_else
(default_value: U) → Union[T, U]¶ If Maybe is empty return default_value, in other case.
- Parameters
default_value (Any) – value to return if Maybe is empty
- Returns
value of Maybe or default_value
- Return type
A
-
classmethod
just
(value: T) → pymonet.maybe.Maybe[~T][T]¶ Create not empty maybe.
- Parameters
mapper (Any) – value to store in Maybe
- Returns
Maybe[Any]
-
map
(mapper: Callable[[T], U]) → Union[pymonet.maybe.Maybe[~U][U], pymonet.maybe.Maybe[NoneType][None]]¶ If Maybe is empty return new empty Maybe, in other case takes mapper function and returns new instance of Maybe with result of mapper.
- Parameters
mapper (Function(A) -> B) – function to call with Maybe value
- Returns
Maybe[B | None]
-
classmethod
nothing
() → pymonet.maybe.Maybe[NoneType][None]¶ Create empty maybe.
- Returns
Maybe[None]
-
to_box
()¶ Transform Maybe to Box.
- Returns
Box monad with previous value when Maybe is not empty, in other case Box with None
- Return type
Box[A | None]
-
to_either
()¶ Transform Maybe to Either.
- Returns
Right monad with previous value when Maybe is not empty, in other case Left with None
- Return type
Either[A | None]
-
to_lazy
()¶ Transform Maybe to Try.
- Returns
Lazy monad with function returning previous value in other case Left with None
- Return type
Lazy[Function() -> (A | None)]
-
to_try
()¶ Transform Maybe to Try.
- Returns
successfully Try with previous value when Maybe is not empty, othercase not successfully Try with None
- Return type
Try[A]
-
to_validation
()¶ Transform Maybe into Validation.
- Returns
successfull Validation monad with previous value or None when Maybe is empty
- Return type
Validation[A, []]
-