コード例 #1
0
ファイル: box.py プロジェクト: przemyslawjanpietrzak/pyMonet
    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)
コード例 #2
0
    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))
コード例 #3
0
    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)
コード例 #4
0
 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)
コード例 #5
0
def test_maybe_if_filter_returns_true_method_should_return_self():
    assert Maybe.just(42).filter(lambda value: value % 2 == 0) == Maybe.just(
        42)
コード例 #6
0
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()
コード例 #7
0
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
コード例 #8
0
def test_maybe_if_filter_returns_false_method_should_return_empty_maybe():
    assert Maybe.just(41).filter(
        lambda value: value % 2 == 0) == Maybe.nothing()
コード例 #9
0
def test_maybe_bind_should_retrun_result_of_mapper_called_with_maybe_value(
        maybe_spy):
    assert Maybe.just(42).bind(increase) == 43
コード例 #10
0
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
コード例 #11
0
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()
コード例 #12
0
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()
コード例 #13
0
 def binder(self, value):
     return Maybe.just(value + 1)
コード例 #14
0
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()
コード例 #15
0
def test_maybe_functor_law(integer):
    FunctorLawTester(functor=Maybe.just(integer),
                     mapper1=lambda value: value + 1,
                     mapper2=lambda value: value + 2).test()