diff options
Diffstat (limited to 'itertoolset.py')
-rw-r--r-- | itertoolset.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/itertoolset.py b/itertoolset.py index bd4e45c..9d5200c 100644 --- a/itertoolset.py +++ b/itertoolset.py @@ -1,4 +1,5 @@ from itertools import * +import collections def roundrobin(*iterables): """roundrobin('ABC', 'D', 'EF') --> A D E B F C""" @@ -36,3 +37,13 @@ def ncycles(iterable, n): def take(n, iterable): "Return first n items of the iterable as a list" return list(islice(iterable, n)) + +def consume(iterator, n=None): + "Advance the iterator n-steps ahead. If n is none, consume entirely." + # Use functions that consume iterators at C speed. + if n is None: + # feed the entire iterator into a zero-length deque + collections.deque(iterator, maxlen=0) + else: + # advance to the empty slice starting at position n + next(islice(iterator, n, n), None) |