예제 #1
0
def test_functor():
    a = Some(2)
    n = Nil()

    actual = a.map(lambda x: x + 2)
    expected = Some(4)
    assert actual == expected

    actual = n.map(lambda x: x + 2)
    expected = Nil()
    assert actual == expected
예제 #2
0
def test_monad_flatten():
    a = Some(Some(3))
    actual = a.flatten()
    expected = Some(3)
    assert actual == expected

    a = Some(Nil())
    actual = a.flatten()
    expected = Nil()
    assert actual == expected

    a = Nil()
    actual = a.flatten()
    expected = Nil()
    assert actual == expected
예제 #3
0
def test_some(obj):
    some = Some(obj)

    assert some.value == obj

    a_obj = some.match(
        some=lambda x: x,
        nil=None
    )
    assert a_obj == obj

    resp = some.match(
        some=12345,
        nil=None
    )
    assert resp == 12345
예제 #4
0
def test_applicative_ap():
    a = Some(3)
    n = Nil()

    fn = Some(lambda x: x - 1)
    actual = fn.ap(a)
    expected = Some(2)
    assert actual == expected

    actual = fn.ap(n)
    expected = Nil()
    assert actual == expected

    fn = Nil()
    actual = fn.ap(a)
    expected = Nil()
    assert actual == expected
예제 #5
0
def test_monad_flat_map():
    a = Some(2)
    actual = a.flat_map(lambda x: Some(x + 3))
    expected = Some(5)
    assert actual == expected

    actual = a.flat_map(lambda _: Nil())
    expected = Nil()
    assert actual == expected

    n = Nil()
    actual = n.flat_map(lambda x: Some(x + 1))
    expected = Nil()
    assert actual == expected
예제 #6
0
def test_semigroup():
    a = Some(1)
    b = Some(2)
    n = Nil()

    actual = a.combine(b)
    expected = Some(3)
    assert actual == expected
    actual = a + b
    assert actual == expected

    actual = a.combine(n)
    expected = n
    assert actual == expected

    actual = n.combine(b)
    expected = n
    assert actual == expected

    actual = n.combine(n)
    expected = n
    assert actual == expected
예제 #7
0
def test_applicative_pure():
    actual = Option.pure(1)
    expected = Some(1)
    assert actual == expected
예제 #8
0
def test_monoid():
    assert Option.unit() == Nil()
    assert Some.unit() == Nil()
    assert Nil.unit() == Nil()