]> granicus.if.org Git - python/commitdiff
Add consume() recipe to itertools docs.
authorRaymond Hettinger <python@rcn.com>
Mon, 9 Mar 2009 11:58:33 +0000 (11:58 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 9 Mar 2009 11:58:33 +0000 (11:58 +0000)
Doc/library/itertools.rst

index baa3566edfaf7e9fda11564347a51919633f2ddc..d4d130fea6d6694504694476d90569f3ccb2ac58 100644 (file)
@@ -635,6 +635,10 @@ which incur interpreter overhead.
        "Return function(0), function(1), ..."
        return imap(function, count(start))
 
+   def consume(iterator, n):
+       "Advance the iterator n-steps ahead. If n is none, consume entirely."
+       collections.deque(islice(iterator, n), maxlen=0)
+
    def nth(iterable, n, default=None):
        "Returns the nth item or a default value"
        return next(islice(iterable, n, None), default)