Commit b8d688cd authored by Raymond Hettinger's avatar Raymond Hettinger

Update itertools recipe for consume().

parent d282b931
......@@ -672,7 +672,13 @@ which incur interpreter overhead.
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
collections.deque(islice(iterator, n), maxlen=0)
# 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 emtpy slice starting at position n
next(islice(iterator, n, n), None)
def nth(iterable, n, default=None):
"Returns the nth item or a default value"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment