def test_key_as_getter(): squares = [(i, i**2) for i in range(5)] pows = [(i, i**2, i**3) for i in range(5)] assert set(join(0, squares, 0, pows)) == set(join(lambda x: x[0], squares, lambda x: x[0], pows)) get = lambda x: (x[0], x[1]) assert set(join([0, 1], squares, [0, 1], pows)) == set(join(get, squares, get, pows)) get = lambda x: (x[0],) assert set(join([0], squares, [0], pows)) == set(join(get, squares, get, pows))
def test_mapcat(): assert list(mapcat(identity, [[1, 2, 3], [4, 5, 6]])) == [1, 2, 3, 4, 5, 6] assert list(mapcat(reversed, [[3, 2, 1, 0], [6, 5, 4], [9, 8, 7]])) == list(range(10)) inc = lambda i: i + 1 assert [4, 5, 6, 7, 8, 9] == list(mapcat(partial(map, inc), [[3, 4, 5], [6, 7, 8]]))
def test_mapcat(): assert (list(mapcat(identity, [[1, 2, 3], [4, 5, 6]])) == [1, 2, 3, 4, 5, 6]) assert (list(mapcat(reversed, [[3, 2, 1, 0], [6, 5, 4], [9, 8, 7]])) == list(range(10))) inc = lambda i: i + 1 assert ([4, 5, 6, 7, 8, 9] == list(mapcat(partial(map, inc), [[3, 4, 5], [6, 7, 8]])))
def groupsizes(total, len): """ Groups of length len that add up to total >>> from kanren.util import groupsizes >>> tuple(groupsizes(4, 2)) ((1, 3), (2, 2), (3, 1)) """ if len == 1: yield (total, ) else: for i in range(1, total - len + 1 + 1): for perm in groupsizes(total - i, len - 1): yield (i, ) + perm
def groupsizes(total, len): """ Groups of length len that add up to total >>> from kanren.util import groupsizes >>> tuple(groupsizes(4, 2)) ((1, 3), (2, 2), (3, 1)) """ if len == 1: yield (total,) else: for i in range(1, total - len + 1 + 1): for perm in groupsizes(total - i, len - 1): yield (i,) + perm
def test_partition_all(): assert list(partition_all(2, [1, 2, 3, 4])) == [(1, 2), (3, 4)] assert list(partition_all(3, range(5))) == [(0, 1, 2), (3, 4)] assert list(partition_all(2, [])) == [] # Regression test: https://github.com/pytoolz/toolz/issues/387 class NoCompare(object): def __eq__(self, other): if self.__class__ == other.__class__: return True raise ValueError() obj = NoCompare() result = [(obj, obj, obj, obj), (obj, obj, obj)] assert list(partition_all(4, [obj]*7)) == result assert list(partition_all(4, iter([obj]*7))) == result
def test_random_sample(): alist = list(range(100)) assert list(random_sample(prob=1, seq=alist, random_state=2016)) == alist mk_rsample = lambda rs=1: list( random_sample(prob=0.1, seq=alist, random_state=rs)) rsample1 = mk_rsample() assert rsample1 == mk_rsample() rsample2 = mk_rsample(1984) randobj = Random(1984) assert rsample2 == mk_rsample(randobj) assert rsample1 != rsample2 assert mk_rsample(object) == mk_rsample(object) assert mk_rsample(object) != mk_rsample(object()) assert mk_rsample(b"a") == mk_rsample(u"a") assert raises(TypeError, lambda: mk_rsample([]))
def test_random_sample(): alist = list(range(100)) assert list(random_sample(prob=1, seq=alist, random_state=2016)) == alist mk_rsample = lambda rs=1: list(random_sample(prob=0.1, seq=alist, random_state=rs)) rsample1 = mk_rsample() assert rsample1 == mk_rsample() rsample2 = mk_rsample(1984) randobj = Random(1984) assert rsample2 == mk_rsample(randobj) assert rsample1 != rsample2 assert mk_rsample(object) == mk_rsample(object) assert mk_rsample(object) != mk_rsample(object()) assert mk_rsample(b"a") == mk_rsample(u"a") assert raises(TypeError, lambda: mk_rsample([]))
def test_remove(): r = remove(iseven, range(5)) assert type(r) is not list assert list(r) == list(filter(isodd, range(5)))
def test_partition_all(): assert list(partition_all(2, [1, 2, 3, 4])) == [(1, 2), (3, 4)] assert list(partition_all(3, range(5))) == [(0, 1, 2), (3, 4)] assert list(partition_all(2, [])) == []
def test_partition(): assert list(partition(2, [1, 2, 3, 4])) == [(1, 2), (3, 4)] assert list(partition(3, range(7))) == [(0, 1, 2), (3, 4, 5)] assert list(partition(3, range(4), pad=-1)) == [(0, 1, 2), (3, -1, -1)] assert list(partition(2, [])) == []
def test_remove(): assert list(remove(iseven, range(5))) == list(filter(isodd, range(5)))
def test_interpose(): assert "a" == first(rest(interpose("a", range(1000000000)))) assert "tXaXrXzXaXn" == "".join(interpose("X", "tarzan")) assert list(interpose(0, itertools.repeat(1, 4))) == [1, 0, 1, 0, 1, 0, 1] assert list(interpose(".", ["a", "b", "c"])) == ["a", ".", "b", ".", "c"]
def test_remove(): assert list(remove(even, range(5))) == list(filter(odd, range(5)))
def test_interpose(): assert "a" == first(rest(interpose("a", range(10000000000)))) assert "tXaXrXzXaXn" == "".join(interpose("X", "tarzan"))
def test_concatv(): assert list(concatv([], [], [])) == [] assert list(take(5, concatv(["a", "b"], range(1000000000)))) == ["a", "b", 0, 1, 2]
def test_concatv(): assert list(concatv([], [], [])) == [] assert (list(take(5, concatv(['a', 'b'], range(1000000000)))) == ['a', 'b', 0, 1, 2])
def test_interpose(): assert "a" == first(rest(interpose("a", range(1000000000)))) assert "tXaXrXzXaXn" == "".join(interpose("X", "tarzan")) assert list(interpose(0, itertools.repeat(1, 4))) == [1, 0, 1, 0, 1, 0, 1] assert list(interpose('.', ['a', 'b', 'c'])) == ['a', '.', 'b', '.', 'c']