コード例 #1
0
def test___mul__():
    x = Iter('ABCD')
    y = Iter('ABCD')
    z = (x * y).collect(list)

    assert len(z) == 16
    assert z == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'),
                 ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'),
                 ('C', 'C'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C'),
                 ('D', 'D')]
コード例 #2
0
def test_permutations():
    x = Iter('ABCD').permutations(r=2).collect(list)

    assert len(x) == 12
    assert x == [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'C'),
                 ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'D'), ('D', 'A'),
                 ('D', 'B'), ('D', 'C')]
コード例 #3
0
def test_count():
    count = Iter.count(start=5, step=2)

    assert next(count) == 5
    assert next(count) == 7
    assert next(count) == 9
    assert next(count) == 11
コード例 #4
0
def test_star_map():
    def pow(x, y):
        return x**y

    x = Iter(range(4)).zip(range(4)).star_map(pow)

    assert list(x) == [0**0, 1**1, 2**2, 3**3]
コード例 #5
0
def test_product():
    x = Iter('ABCD').product(repeat=2).collect(list)

    assert len(x) == 16
    assert x == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'),
                 ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'),
                 ('C', 'C'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C'),
                 ('D', 'D')]
コード例 #6
0
def test_zip_longest_other_way():
    x = Iter(range(2)).zip_longest(range(3))

    assert list(x) == [(0, 0), (1, 1), (None, 2)]
コード例 #7
0
def char_iter():
    return Iter(c for c in HELLO_WORLD)
コード例 #8
0
def test_enumerate():
    d = Iter(('a', 'b', 'c'))

    assert tuple(d.enumerate()) == ((0, 'a'), (1, 'b'), (2, 'c'))
コード例 #9
0
def test_on_list():
    d = Iter([0, 1, 2, 3, 4])

    assert tuple(d.map(lambda x: 2 * x)) == (0, 2, 4, 6, 8)
コード例 #10
0
def test_combinations_with_replacement():
    x = Iter('ABCD').combinations_with_replacement(2).collect(list)

    assert len(x) == 10
    assert x == [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'B'),
                 ('B', 'C'), ('B', 'D'), ('C', 'C'), ('C', 'D'), ('D', 'D')]
コード例 #11
0
def test_cycle():
    x = Iter('ABCD').cycle()
    cycle = itertools.cycle('ABCD')

    for _ in range(1000):
        assert next(x) == next(cycle)
コード例 #12
0
def int_iter():
    return Iter(range(5))
コード例 #13
0
def test_combinations():
    x = Iter('ABCD').combinations(2).collect(list)

    assert len(x) == 6
    assert x == [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'),
                 ('C', 'D')]
コード例 #14
0
def test_repeat():
    repeat = Iter.repeat(True)

    for _ in range(100):
        assert next(repeat)
コード例 #15
0
def test_zip_longest():
    x = Iter(range(3)).zip_longest(range(2))

    assert list(x) == [(0, 0), (1, 1), (2, None)]
コード例 #16
0
def test_unzip():
    foo = Iter(range(3)).zip(range(3), range(3))

    a, b, c = foo.unzip()

    assert a == b == c == [0, 1, 2]
コード例 #17
0
def test_matmul():
    a = Iter(range(3))
    b = Iter(range(4))

    return a @ b == 1**2 + 2**2 + 3**2
コード例 #18
0
def test_dot():
    a = Iter(range(3))
    b = Iter(range(4))

    return a.dot(b) == 1**2 + 2**2 + 3**2
コード例 #19
0
def test_compress():
    x = Iter('hello!')
    selectors = [0, 1, 0, 1, 1, 0]

    assert ''.join(x.compress(selectors)) == 'elo'
コード例 #20
0
def test_or():
    x = Iter(range(3)) | range(2)

    assert list(x) == [(0, 0), (1, 1), (2, None)]
コード例 #21
0
from hypoxia import Iter

x = (c for c in 'hello')

print(x)

h = Iter(x)
print(h)

# y = h.map(lambda c: c.upper())
y = h.filter(lambda c: c != 'l')
print(y)

print(''.join(y))

# x = (c for c in 'hello')
コード例 #22
0
def test_repeat_limit():
    repeat = Iter.repeat(True, 10)

    assert list(repeat) == [True for _ in range(10)]