diff --git a/pydu/iter.py b/pydu/iter.py index d402a61..49755b7 100644 --- a/pydu/iter.py +++ b/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) diff --git a/tests/test_iter.py b/tests/test_iter.py index de11ed9..86fd8d1 100644 --- a/tests/test_iter.py +++ b/tests/test_iter.py @@ -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)