コード例 #1
0
def test_Maybe_wrap():
    @Maybe.as_wrapper(checker=lambda x: x % 2)
    def derp(x):
        'Subtract one from a number.'
        return x - 1

    assert derp.__name__ == 'derp'
    assert derp.__doc__ == 'Subtract one from a number.'
    assert derp(2) == Just(1)
    assert derp(3) == Nothing
    assert derp(12).fmap(lambda x: x//2).or_else(1) == Just(5)
コード例 #2
0
def test_Nothing_propagates():
    assert (Just(2) >> n_add_two >> add_two) is Nothing
コード例 #3
0
def test_Just_chain_many():
    x = Just(2).fmap(add_two).bind(m_add_two).filter(lambda x: not x % 3) \
        .or_else(Just(3))

    assert x == Just(6)
コード例 #4
0
def test_Nothing_or_call():
    assert Nothing.or_call(add_two, 2) == Just(4)
コード例 #5
0
def test_mapM():
    assert lifted.mapM(m_add_two, *range(5)) == Just(List(2, 3, 4, 5, 6))
コード例 #6
0
def test_mcons():
    assert lifted.mcons(Just(0), Just(List(1, 2, 3))) == Just(List(0, 1, 2, 3))
    assert lifted.mcons(Nothing, Just(List(0))) is Nothing
コード例 #7
0
def test_lift_func():
    assert lifted.lift(add_two, 2, Just) == Just(4)
コード例 #8
0
def test_unit_func():
    assert lifted.unit(2, Just) == Just(2)
コード例 #9
0
def test_Just_bind():
    assert (Just(2) >> m_add_two) == Just(4)
コード例 #10
0
def test_just_apply():
    assert Just(add_two).apply(Just(2)) == Just(4)
コード例 #11
0
def test_just_fmap():
    assert Just(2).fmap(add_two) == Just(4)
コード例 #12
0
def test_Maybe_to_Either():
    assert Just(4).to_either(None) == Right(4)
    assert Nothing.to_either('failure') == Left('failure')
コード例 #13
0
def test_truthiness():
    assert not bool(Nothing)
    assert bool(Just(1))
    assert Nothing == Nothing
    assert Just(1) == Just(1)
コード例 #14
0
def test_maybe_custom_check():
    c = lambda x: x & 1
    assert Maybe(1, checker=c) == Just(1)
    assert Maybe(2, checker=c) is Nothing
コード例 #15
0
def test_identity():
    assert pure.identity(2) == 2
    assert Just(2).fmap(pure.identity) == Just(2)
コード例 #16
0
def test_Just_get_or():
    assert Just(2).get_or(3) == 2
コード例 #17
0
from pynads import Just, List, Nothing
from pynads.funcs import lifted

add_two = lambda x: x + 2
curried_add = lambda x: lambda y: x + y
m_add_two = lambda x: Just(x + 2)


def test_fmap_func():
    assert lifted.fmap(add_two, Just(2)) == Just(4)


def test_unit_func():
    assert lifted.unit(2, Just) == Just(2)


def test_multiapply():
    j = Just.unit(curried_add)
    js = [Just(2), Just(2)]
    assert lifted.multiapply(j, *js) == Just(4)


def test_lift_func():
    assert lifted.lift(add_two, 2, Just) == Just(4)


def test_multibind():
    j = Just.unit(2)
    assert lifted.multibind(j, m_add_two, m_add_two) == Just(6)

コード例 #18
0
def test_Just_get_or_call():
    assert Just(2).get_or_call(add_two, 1) == 2
コード例 #19
0
def test_multiapply():
    j = Just.unit(curried_add)
    js = [Just(2), Just(2)]
    assert lifted.multiapply(j, *js) == Just(4)
コード例 #20
0
def test_Just_or_else():
    assert Just(2).or_else(Nothing) == Just(2)
コード例 #21
0
def test_multibind():
    j = Just.unit(2)
    assert lifted.multibind(j, m_add_two, m_add_two) == Just(6)
コード例 #22
0
def test_Just_or_call():
    assert Just(2).or_call(m_add_two, 1) == Just(2)
コード例 #23
0
def test_sequence():
    justs = [Just(x) for x in range(5)]
    assert lifted.sequence(*justs) == Just(List(0, 1, 2, 3, 4))
    assert lifted.sequence(Nothing, *justs) is Nothing
コード例 #24
0
def test_Just_filter():
    assert Just(2).filter(lambda x: x % 2 == 0) == Just(2)
コード例 #25
0
def test_fmap_func():
    assert lifted.fmap(add_two, Just(2)) == Just(4)
コード例 #26
0
def test_Nothing_or_else():
    assert Nothing.or_else(4) == Just(4)