[Python-checkins] r77721 - python/branches/release26-maint/Doc/library/itertools.rst

raymond.hettinger python-checkins at python.org
Sun Jan 24 04:34:56 CET 2010


Author: raymond.hettinger
Date: Sun Jan 24 04:34:56 2010
New Revision: 77721

Log:
Issue 7764: Improve recipe for itertools.consume().

Modified:
   python/branches/release26-maint/Doc/library/itertools.rst

Modified: python/branches/release26-maint/Doc/library/itertools.rst
==============================================================================
--- python/branches/release26-maint/Doc/library/itertools.rst	(original)
+++ python/branches/release26-maint/Doc/library/itertools.rst	Sun Jan 24 04:34:56 2010
@@ -641,7 +641,13 @@
 
    def consume(iterator, n):
        "Advance the iterator n-steps ahead. If n is none, consume entirely."
-       collections.deque(islice(iterator, n), maxlen=0)
+       # The technique uses objects 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"
@@ -751,3 +757,10 @@
        # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
        # unique_justseen('ABBCcAD', str.lower) --> A B C A D
        return imap(next, imap(itemgetter(1), groupby(iterable, key)))
+
+Note, many of the above recipes can be optimized by replacing global lookups
+with local variables defined as default values.  For example, the
+*dotproduct* recipe can be written as:
+
+   def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):
+       return sum(imap(mul, vec1, vec2))


More information about the Python-checkins mailing list