mirror of https://github.com/flaggo/pydu.git
add iter.all, iter.any and their test
This commit is contained in:
parent
47531166cb
commit
7cee0d9267
17
pydu/iter.py
17
pydu/iter.py
|
@ -1,4 +1,5 @@
|
|||
"""iteration tools"""
|
||||
from compat import builtins
|
||||
|
||||
|
||||
def first(iterable):
|
||||
|
@ -17,3 +18,19 @@ def last(iterable):
|
|||
for item in iterable:
|
||||
pass
|
||||
return item
|
||||
|
||||
|
||||
def all(iterable, predicate):
|
||||
"""
|
||||
Returns True if all elements in the given iterable are True for the
|
||||
given predicate function.
|
||||
"""
|
||||
return builtins.all(predicate(x) for x in iterable)
|
||||
|
||||
|
||||
def any(iterable, predicate):
|
||||
"""
|
||||
Returns True if any element in the given iterable is True for the
|
||||
given predicate function.
|
||||
"""
|
||||
return builtins.any(predicate(x) for x in iterable)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import pytest
|
||||
from pydu.iter import first, last
|
||||
from pydu.iter import first, last, all, any
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -13,3 +13,13 @@ from pydu.iter import first, last
|
|||
def test_first_last(iterable):
|
||||
assert first(iterable) == 1
|
||||
assert last(iterable) == 2
|
||||
|
||||
|
||||
def test_all():
|
||||
assert all([0, 1, 2], lambda x: x+1)
|
||||
assert not all([0, 1, 2], lambda x: x)
|
||||
|
||||
|
||||
def test_any():
|
||||
assert any([-1, -1, 0], lambda x: x+1)
|
||||
assert not any([-1, -1, -1], lambda x: x + 1)
|
||||
|
|
Loading…
Reference in New Issue