]> granicus.if.org Git - python/commitdiff
Update itertools recipe for consume().
authorRaymond Hettinger <python@rcn.com>
Sun, 28 Mar 2010 18:25:01 +0000 (18:25 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 28 Mar 2010 18:25:01 +0000 (18:25 +0000)
Doc/library/itertools.rst

index 4f82a27d11dda3f1ced70b3cf4bda41fc7c88e62..f83e9762a22d20cff51eb2c08cf5c7d695f226b7 100644 (file)
@@ -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"