[Python-checkins] r46427 - sandbox/trunk/Doc/functional.rst

Jim Jewett jimjjewett at gmail.com
Mon May 29 01:31:47 CEST 2006


I think the dropwhile and takewhile examples should demonstrate
whether a true/false sticks.

So itertools.takewhile(is_even, itertools.count()) =>
    0

2 would pass, but it doesn't get there, because the 1 stops it

itertools.dropwhile(is_even, itertools.count()) =>
    1, 2, 3

because once the is_even(1) is false, the predicate stops checking.

-jJ


On 5/26/06, andrew.kuchling <python-checkins at python.org> wrote:
> Author: andrew.kuchling
> Date: Sat May 27 02:32:40 2006
> New Revision: 46427
>
> Modified:
>    sandbox/trunk/Doc/functional.rst
> Log:
> Describe more functions
>
> Modified: sandbox/trunk/Doc/functional.rst
> ==============================================================================
> --- sandbox/trunk/Doc/functional.rst    (original)
> +++ sandbox/trunk/Doc/functional.rst    Sat May 27 02:32:40 2006
> @@ -725,14 +725,54 @@
>      =>
>        /usr/bin/java, /bin/python, /usr/bin/perl, /usr/bin/ruby
>
> +Another group of functions chooses a subset of an iterator's elements
> +based on a **predicate**, a function that returns the truth value of
> +some condition.
> +
> +``itertools.ifilter(predicate, iter)`` returns all the elements for
> +which the predicate returns true::
> +
> +    def is_even(x):
> +        return (x % 2) == 0
> +
> +    itertools.ifilter(is_even, itertools.count()) =>
> +      0, 2, 4, 6, 8, 10, 12, 14, ...
> +
> +``itertools.ifilterfalse(predicate, iter)`` is the opposite,
> +returning all elements for which the predicate returns false::
> +
> +    itertools.ifilterfalse(is_even, itertools.count()) =>
> +      1, 3, 5, 7, 9, 11, 13, 15, ...
> +
> +``itertools.takewhile(predicate, iter)`` returns elements for as long
> +as the predicate returns true.  Once the predicate returns false,
> +the iterator will signal the end of its results.
> +
> +::
> +
> +    def less_than_10(x):
> +        return (x < 10)
> +
> +    itertools.takewhile(less_than_10, itertools.count()) =>
> +      0, 1, 2, 3, 4, 5, 6, 7, 8, 9
> +
> +``itertools.dropwhile(predicate, iter)`` discards elements while the
> +predicate returns true, and then returns the rest of the iterable's
> +results.
> +
> +::
> +
> +    itertools.dropwhile(less_than_10, itertools.count()) =>
> +      10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...
> +
> +
> +
> +
> +
>
>
>  .. comment
>
> -    ifilter
> -    ifilterfalse
> -    takewhile
> -    dropwhile
>      groupby
>
>
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>


More information about the Python-checkins mailing list