mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Update itertools recipe for consume().
This commit is contained in:
parent
d282b931a3
commit
b8d688cd7c
1 changed files with 7 additions and 1 deletions
|
@ -672,7 +672,13 @@ which incur interpreter overhead.
|
||||||
|
|
||||||
def consume(iterator, n):
|
def consume(iterator, n):
|
||||||
"Advance the iterator n-steps ahead. If n is none, consume entirely."
|
"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):
|
def nth(iterable, n, default=None):
|
||||||
"Returns the nth item or a default value"
|
"Returns the nth item or a default value"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue