]> granicus.if.org Git - python/commitdiff
Minor updates:
authorRaymond Hettinger <python@rcn.com>
Wed, 18 Jun 2003 19:25:37 +0000 (19:25 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 18 Jun 2003 19:25:37 +0000 (19:25 +0000)
* Updated comment on design of imap()
* Added untraversed object in izip() structure
* Replaced the pairwise() example with a more general window() example

Doc/lib/libitertools.tex
Lib/test/test_itertools.py
Modules/itertoolsmodule.c

index f291fe34b2d20431a2f86f61920dfd46ae22cbc8..e146c6c989c07c670a1273ff9fb7eed1dc8c6d7b 100644 (file)
@@ -358,10 +358,6 @@ from building blocks.
 ...     "Returns True if pred(x) is False for every element in the iterable"
 ...     return not nth(ifilter(pred, seq), 0)
 
->>> def pairwise(seq):
-...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
-...     return izip(seq, islice(seq,1,None))
-
 >>> def padnone(seq):
 ...     "Returns the sequence elements and then returns None indefinitely"
 ...     return chain(seq, repeat(None))
@@ -373,4 +369,15 @@ from building blocks.
 >>> def dotproduct(vec1, vec2):
 ...     return sum(imap(operator.mul, vec1, vec2))
 
+>>> def window(seq, n=2):
+...     "Returns a sliding window (of width n) over data from the iterable"
+...     "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
+...     it = iter(seq)
+...     result = tuple(islice(it, n))
+...     if len(result) == n:
+...         yield result    
+...     for elem in it:
+...         result = result[1:] + (elem,)
+...         yield result
+
 \end{verbatim}
index a4ad845b4f0833cc1153a5a3b4fb3659330a10e8..846a690836f88e3045d5481329e334a7aef34fd3 100644 (file)
@@ -427,10 +427,6 @@ Samuele
 ...     "Returns True if pred(x) is False for every element in the iterable"
 ...     return not nth(ifilter(pred, seq), 0)
 
->>> def pairwise(seq):
-...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
-...     return izip(seq, islice(seq,1,len(seq)))
-
 >>> def padnone(seq):
 ...     "Returns the sequence elements and then returns None indefinitely"
 ...     return chain(seq, repeat(None))
@@ -442,6 +438,16 @@ Samuele
 >>> def dotproduct(vec1, vec2):
 ...     return sum(imap(operator.mul, vec1, vec2))
 
+>>> def window(seq, n=2):
+...     "Returns a sliding window (of width n) over data from the iterable"
+...     "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
+...     it = iter(seq)
+...     result = tuple(islice(it, n))
+...     if len(result) == n:
+...         yield result
+...     for elem in it:
+...         result = result[1:] + (elem,)
+...         yield result
 
 This is not part of the examples but it tests to make sure the definitions
 perform as purported.
@@ -473,9 +479,12 @@ True
 >>> no(lambda x: x%2==0, [1, 2, 5, 9])
 False
 
->>> list(pairwise('abc'))
+>>> list(window('abc'))
 [('a', 'b'), ('b', 'c')]
 
+>>> list(window('abc',5))
+[]
+
 >>> list(islice(padnone('abc'), 0, 6))
 ['a', 'b', 'c', None, None, None]
 
index 0a20c1b568220397ed14e34224ba46723d8c5d26..fae4511ef7be9d747b5a5f0483d760dce920f7ad 100644 (file)
@@ -882,7 +882,7 @@ the following reasons:
      None.  
 
   4) If a need does arise, it can be met by __builtins__.map() or by 
-     writing a generator.
+     writing:  chain(iterable, repeat(None)).
 
   5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
 */
@@ -1574,8 +1574,18 @@ izip_dealloc(izipobject *lz)
 static int
 izip_traverse(izipobject *lz, visitproc visit, void *arg)
 {
-       if (lz->ittuple)
-               return visit(lz->ittuple, arg);
+       int err;
+
+       if (lz->ittuple) {
+               err = visit(lz->ittuple, arg);
+               if (err)
+                       return err;
+       }
+       if (lz->result) {
+               err = visit(lz->result, arg);
+               if (err)
+                       return err;
+       }
        return 0;
 }