From d591f666de4c06cd36624fd513d5bc7d8c031ec8 Mon Sep 17 00:00:00 2001
From: Raymond Hettinger <python@rcn.com>
Date: Sun, 26 Oct 2003 15:34:50 +0000
Subject: Replace the window() example with pairwise() which demonstrates
 tee().

---
 Doc/lib/libitertools.tex   | 14 ++++----------
 Lib/test/test_itertools.py | 23 ++++++++++-------------
 2 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex
index eb6bc49..27e4e45 100644
--- a/Doc/lib/libitertools.tex
+++ b/Doc/lib/libitertools.tex
@@ -405,15 +405,9 @@ def repeatfunc(func, times=None, *args):
     else:
         return starmap(func, repeat(args, times))
 
-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
+def pairwise(iterable):
+    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
+    a, b = tee(iterable)
+    return izip(a, islice(b, 1, None))
 
 \end{verbatim}
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index e12aa41..07219a9 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -623,16 +623,10 @@ Samuele
 ...     else:
 ...         return starmap(func, repeat(args, times))
 
->>> 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
+>>> def pairwise(iterable):
+...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
+...     a, b = tee(iterable)
+...     return izip(a, islice(b, 1, None))
 
 This is not part of the examples but it tests to make sure the definitions
 perform as purported.
@@ -681,10 +675,13 @@ False
 >>> take(5, imap(int, repeatfunc(random.random)))
 [0, 0, 0, 0, 0]
 
->>> list(window('abc'))
-[('a', 'b'), ('b', 'c')]
+>>> list(pairwise('abcd'))
+[('a', 'b'), ('b', 'c'), ('c', 'd')]
 
->>> list(window('abc',5))
+>>> list(pairwise([]))
+[]
+
+>>> list(pairwise('a'))
 []
 
 >>> list(islice(padnone('abc'), 0, 6))
-- 
cgit v0.12

