Promote compress() from a recipe to being a regular itertool.
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 51fdf89..0dc0ae4 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -298,7 +298,7 @@
Section 4.6.3, Exercise 19*\.
* To enumerate all distinct multisets of a given size over a given set of
- elements, see the :func:`combinations_with_replacement` function in the
+ elements, see :func:`combinations_with_replacement` in the
:ref:`itertools-recipes` for itertools::
map(Counter, combinations_with_replacement('ABC', 2)) --> AA AB AC BB BC CC
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index aef3f6a..903284e 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -139,6 +139,20 @@
.. versionadded:: 2.6
+.. function:: compress(data, selectors)
+
+ Make an iterator that filters elements from *data* returning only those that
+ have a corresponding element in *selectors* that evaluates to ``True``.
+ Stops when either the *data* or *selectors* iterables have been exhausted.
+ Equivalent to::
+
+ def compress(data, selectors):
+ # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
+ return (d for d, s in izip(data, selectors) if s)
+
+ .. versionadded:: 2.7
+
+
.. function:: count([n])
Make an iterator that returns consecutive integers starting with *n*. If not
@@ -679,10 +693,6 @@
for n in xrange(2**len(pairs)):
yield set(x for m, x in pairs if m&n)
- def compress(data, selectors):
- "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
- return (d for d, s in izip(data, selectors) if s)
-
def combinations_with_replacement(iterable, r):
"combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
# number items returned: (n+r-1)! / r! / (n-1)!