summaryrefslogtreecommitdiff
path: root/itertoolset.py
diff options
context:
space:
mode:
authorStan Seibert <stan@mtrr.org>2011-08-11 17:44:43 -0400
committerStan Seibert <stan@mtrr.org>2011-08-11 17:44:43 -0400
commit8487c83d39b12bd830325202a60848df0adf02f5 (patch)
tree54ae0ce9f8213ca045aa6522bd46a9e1105927e7 /itertoolset.py
parent2b7ee5fb5a20fadfa24766442d2d3609ed291b63 (diff)
parent00aa07a12f8f00e29a3a04f2850ccd4fc91cc951 (diff)
downloadchroma-8487c83d39b12bd830325202a60848df0adf02f5.tar.gz
chroma-8487c83d39b12bd830325202a60848df0adf02f5.tar.bz2
chroma-8487c83d39b12bd830325202a60848df0adf02f5.zip
merge
Diffstat (limited to 'itertoolset.py')
-rw-r--r--itertoolset.py11
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)