diff --git a/toolz/tests/test_utils.py b/toolz/tests/test_utils.py index e7a0eaba..2004fe6a 100644 --- a/toolz/tests/test_utils.py +++ b/toolz/tests/test_utils.py @@ -1,6 +1,15 @@ -from toolz.utils import raises +from toolz.utils import raises, consume def test_raises(): assert raises(ZeroDivisionError, lambda: 1 / 0) assert not raises(ZeroDivisionError, lambda: 1) + + +def test_consume(): + l = [1, 2, 3] + assert consume(l) is None + il = iter(l) + assert consume(il) is None + assert raises(StopIteration, lambda: next(il)) + assert raises(TypeError, lambda: consume(1)) diff --git a/toolz/utils.py b/toolz/utils.py index 1002c464..18e0c571 100644 --- a/toolz/utils.py +++ b/toolz/utils.py @@ -1,3 +1,11 @@ +from collections import deque + + +def consume(seq): + """Efficently consume an iterable""" + deque(seq, maxlen=0) + + def raises(err, lamda): try: lamda()