def to_maybe(self): """ Transform Box into not empty Maybe. :returns: non empty Maybe monad with previous value :rtype: Maybe[A] """ from pymonet.maybe import Maybe return Maybe.just(self.value)
def to_maybe(self, *args): """ Transform Lazy into not empty Maybe with constructor_fn result. :returns: not empty Maybe monad with constructor_fn result :rtype: Maybe[A] """ from pymonet.maybe import Maybe return Maybe.just(self.get(*args))
def to_maybe(self): """ Transform Either to Maybe. :returns: Maybe with previous value :rtype: Maybe[A] """ from pymonet.maybe import Maybe return Maybe.just(self.value)
def to_maybe_test(self): if self.is_fail: assert self.monad(self.value).to_maybe() == Maybe.nothing() else: assert self.monad(self.value).to_maybe() == Maybe.just(self.value)
def test_maybe_if_filter_returns_true_method_should_return_self(): assert Maybe.just(42).filter(lambda value: value % 2 == 0) == Maybe.just( 42)
def test_maybe_monad_law(integer): MonadLawTester(monad=Maybe.just, value=integer, mapper1=lambda value: Maybe.just(value + 1), mapper2=lambda value: Maybe.just(value + 2)).test()
def test_maybe_is_nothing_should_return_proper_boolean(integer): assert Maybe.just(integer).is_nothing is False assert Maybe.nothing().is_nothing is True
def test_maybe_if_filter_returns_false_method_should_return_empty_maybe(): assert Maybe.just(41).filter( lambda value: value % 2 == 0) == Maybe.nothing()
def test_maybe_bind_should_retrun_result_of_mapper_called_with_maybe_value( maybe_spy): assert Maybe.just(42).bind(increase) == 43
def test_maybe_get_or_else_method_should_return_maybe_value_when_monad_is_not_empty( integer): assert Maybe.just(integer).get_or_else(0) is integer
def test_maybe_map_operator_should_be_applied_only_on_just_value(integer): assert Maybe.just(42).map(increase) == Maybe.just(43) assert Maybe.nothing().map(increase) == Maybe.nothing()
def test_maybe_eq_operator_should_compare_values(integer): assert Maybe.just(integer) == Maybe.just(integer) assert Maybe.just(None) == Maybe.just(None) assert Maybe.just(integer) != Maybe.nothing()
def binder(self, value): return Maybe.just(value + 1)
def test_maybe_ap_on_empty_maybe_should_not_be_applied(): def lambda_fn(): raise TypeError assert Maybe.nothing().ap(Maybe.just(lambda_fn)) == Maybe.nothing() assert Maybe.just(42).ap(Maybe.nothing()) == Maybe.nothing()
def test_maybe_functor_law(integer): FunctorLawTester(functor=Maybe.just(integer), mapper1=lambda value: value + 1, mapper2=lambda value: value + 2).test()