def test_build_chessboard(): # shamelessly stolen from: # https://github.com/dustingetz/pymonads/blob/master/list.py ranks = 'abcdefg' files = range(1, 9) l = List(*ranks) >> (lambda r: List(*files) >> (lambda f: List.unit( (r, f)))) hardway = tuple((r, f) for r in ranks for f in files) sliced = List(('a', 1), ('a', 2), ('a', 3)) assert l.v == hardway assert l[:3] == sliced
def test_writer_bind(): w = Writer.unit(2) >> m_add_two assert w.v == (4, List.unit('added two'))
from pynads import Writer, Mempty, List from pynads.funcs import identity, multibind import pytest add_two = lambda x: x + 2 m_add_two = lambda x: Writer(x + 2, List.unit('added two')) str_add_two = lambda x: Writer(add_two(x), ' added two') int_add_two = lambda x: Writer(add_two(x), 4) dict_add_two = lambda x: Writer(add_two(x), {'added': 2}) dict_div_two = lambda x: Writer(x // 2, {'divided': 2}) def test_writer_unit(): w = Writer.unit(2) assert w.v == (2, Mempty) def test_fmap_id(): w = Writer.unit(2).fmap(identity) assert w.v == (2, Mempty) def test_writer_fmap(): w = Writer.unit(2).fmap(add_two) assert w.v == (4, Mempty) def test_writer_apply(): w = (Writer.unit(add_two)) * (Writer.unit(2)) assert w.v == (4, Mempty)
def test_List_unit(): assert List.unit(1).v == (1, ) assert List.unit('fred').v == ('fred', ) assert List.unit([1, 2, 3]).v == ([1, 2, 3], )
def test_List_is_monoidal(): ls = [List.unit(x) for x in [1, 2, 3]] assert List.mappend(ls[0], List.mappend(ls[1], ls[2])) == \ List.mappend(List.mappend(ls[0], ls[1]), ls[2])
def test_List_mconcat(): ls = [List.unit(x) for x in range(4)] final = List.mconcat(*ls) assert final == List(0, 1, 2, 3)
def test_List_monoidal_add(): l = List.unit(1) assert l + (List.unit(2)) == List(1, 2) assert l + (2, ) == List(1, 2)
def test_List_iadd(): l1 = l2 = List.unit(1) l1 += List.unit(2) assert l1 == List(1, 2) assert l1 is not l2
def test_List_eq(): assert List(1, 2, 3) == List(1, 2, 3) assert List.unit(1) != List(1, 2)