summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libcollections.tex
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-04-30 22:52:50 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-04-30 22:52:50 (GMT)
commitf5f9a370d47de6239cd766c30da33f15edd1a460 (patch)
tree9a49a49a1af3414d8e4e3456ab3126f30581453e /Doc/lib/libcollections.tex
parent6fbf703fa2ae593be4aeaef8239594e7aa6e3bb5 (diff)
downloadcpython-f5f9a370d47de6239cd766c30da33f15edd1a460.zip
cpython-f5f9a370d47de6239cd766c30da33f15edd1a460.tar.gz
cpython-f5f9a370d47de6239cd766c30da33f15edd1a460.tar.bz2
Add an example application to the docs.
Diffstat (limited to 'Doc/lib/libcollections.tex')
-rw-r--r--Doc/lib/libcollections.tex33
1 files changed, 31 insertions, 2 deletions
diff --git a/Doc/lib/libcollections.tex b/Doc/lib/libcollections.tex
index 14e3bf5..c4ba84c 100644
--- a/Doc/lib/libcollections.tex
+++ b/Doc/lib/libcollections.tex
@@ -67,7 +67,7 @@ Deque objects support the following methods:
\begin{methoddesc}{rotate}{n}
Rotate the deque \var{n} steps to the right. If \var{n} is
negative, rotate to the left. Rotating one step to the right
- is equivalent to: \samp{d.appendleft(d.pop())}.
+ is equivalent to: \samp{d.appendleft(d.pop())}.
\end{methoddesc}
In addition to the above, deques support iteration, pickling, \samp{len(d)},
@@ -128,5 +128,34 @@ IndexError: pop from an empty deque
>>> d.extendleft('abc') # extendleft() reverses the input order
>>> d
deque(['c', 'b', 'a'])
+\end{verbatim}
-\end{verbatim}
+
+A roundrobin task server can be built from a \class{deque} using
+\method{popleft()} to select the current task and \method{append()}
+to add it back to the tasklist if the input stream is not exhausted:
+
+\begin{verbatim}
+def roundrobin(*iterables):
+ pending = deque(iter(i) for i in iterables)
+ while pending:
+ task = pending.popleft()
+ try:
+ yield task.next()
+ except StopIteration:
+ continue
+ pending.append(task)
+
+>>> for value in roundrobin('abc', 'd', 'efgh'):
+ print value
+
+a
+d
+e
+b
+f
+c
+g
+h
+
+\end{verbatim}