Commit db8707c8 authored by Sergey Fedoseev's avatar Sergey Fedoseev Committed by Mariatta

Make code examples in Functional Programming HOWTO to be PEP 8 compliant. (GH-8646)

parent d2ac4002
...@@ -198,7 +198,7 @@ for it. ...@@ -198,7 +198,7 @@ for it.
You can experiment with the iteration interface manually: You can experiment with the iteration interface manually:
>>> L = [1,2,3] >>> L = [1, 2, 3]
>>> it = iter(L) >>> it = iter(L)
>>> it #doctest: +ELLIPSIS >>> it #doctest: +ELLIPSIS
<...iterator object at ...> <...iterator object at ...>
...@@ -229,7 +229,7 @@ iterator. These two statements are equivalent:: ...@@ -229,7 +229,7 @@ iterator. These two statements are equivalent::
Iterators can be materialized as lists or tuples by using the :func:`list` or Iterators can be materialized as lists or tuples by using the :func:`list` or
:func:`tuple` constructor functions: :func:`tuple` constructor functions:
>>> L = [1,2,3] >>> L = [1, 2, 3]
>>> iterator = iter(L) >>> iterator = iter(L)
>>> t = tuple(iterator) >>> t = tuple(iterator)
>>> t >>> t
...@@ -238,10 +238,10 @@ Iterators can be materialized as lists or tuples by using the :func:`list` or ...@@ -238,10 +238,10 @@ Iterators can be materialized as lists or tuples by using the :func:`list` or
Sequence unpacking also supports iterators: if you know an iterator will return Sequence unpacking also supports iterators: if you know an iterator will return
N elements, you can unpack them into an N-tuple: N elements, you can unpack them into an N-tuple:
>>> L = [1,2,3] >>> L = [1, 2, 3]
>>> iterator = iter(L) >>> iterator = iter(L)
>>> a,b,c = iterator >>> a, b, c = iterator
>>> a,b,c >>> a, b, c
(1, 2, 3) (1, 2, 3)
Built-in functions such as :func:`max` and :func:`min` can take a single Built-in functions such as :func:`max` and :func:`min` can take a single
...@@ -411,7 +411,7 @@ lengths of all the sequences. If you have two lists of length 3, the output ...@@ -411,7 +411,7 @@ lengths of all the sequences. If you have two lists of length 3, the output
list is 9 elements long: list is 9 elements long:
>>> seq1 = 'abc' >>> seq1 = 'abc'
>>> seq2 = (1,2,3) >>> seq2 = (1, 2, 3)
>>> [(x, y) for x in seq1 for y in seq2] #doctest: +NORMALIZE_WHITESPACE >>> [(x, y) for x in seq1 for y in seq2] #doctest: +NORMALIZE_WHITESPACE
[('a', 1), ('a', 2), ('a', 3), [('a', 1), ('a', 2), ('a', 3),
('b', 1), ('b', 2), ('b', 3), ('b', 1), ('b', 2), ('b', 3),
...@@ -479,7 +479,7 @@ Here's a sample usage of the ``generate_ints()`` generator: ...@@ -479,7 +479,7 @@ Here's a sample usage of the ``generate_ints()`` generator:
File "stdin", line 2, in generate_ints File "stdin", line 2, in generate_ints
StopIteration StopIteration
You could equally write ``for i in generate_ints(5)``, or ``a,b,c = You could equally write ``for i in generate_ints(5)``, or ``a, b, c =
generate_ints(3)``. generate_ints(3)``.
Inside a generator function, ``return value`` causes ``StopIteration(value)`` Inside a generator function, ``return value`` causes ``StopIteration(value)``
...@@ -695,17 +695,17 @@ truth values of an iterable's contents. :func:`any` returns ``True`` if any ele ...@@ -695,17 +695,17 @@ truth values of an iterable's contents. :func:`any` returns ``True`` if any ele
in the iterable is a true value, and :func:`all` returns ``True`` if all of the in the iterable is a true value, and :func:`all` returns ``True`` if all of the
elements are true values: elements are true values:
>>> any([0,1,0]) >>> any([0, 1, 0])
True True
>>> any([0,0,0]) >>> any([0, 0, 0])
False False
>>> any([1,1,1]) >>> any([1, 1, 1])
True True
>>> all([0,1,0]) >>> all([0, 1, 0])
False False
>>> all([0,0,0]) >>> all([0, 0, 0])
False False
>>> all([1,1,1]) >>> all([1, 1, 1])
True True
...@@ -764,7 +764,7 @@ which defaults to 0, and the interval between numbers, which defaults to 1:: ...@@ -764,7 +764,7 @@ which defaults to 0, and the interval between numbers, which defaults to 1::
a provided iterable and returns a new iterator that returns its elements from a provided iterable and returns a new iterator that returns its elements from
first to last. The new iterator will repeat these elements infinitely. :: first to last. The new iterator will repeat these elements infinitely. ::
itertools.cycle([1,2,3,4,5]) => itertools.cycle([1, 2, 3, 4, 5]) =>
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
:func:`itertools.repeat(elem, [n]) <itertools.repeat>` returns the provided :func:`itertools.repeat(elem, [n]) <itertools.repeat>` returns the provided
...@@ -875,7 +875,7 @@ iterable's results. :: ...@@ -875,7 +875,7 @@ iterable's results. ::
iterators and returns only those elements of *data* for which the corresponding iterators and returns only those elements of *data* for which the corresponding
element of *selectors* is true, stopping whenever either one is exhausted:: element of *selectors* is true, stopping whenever either one is exhausted::
itertools.compress([1,2,3,4,5], [True, True, False, False, True]) => itertools.compress([1, 2, 3, 4, 5], [True, True, False, False, True]) =>
1, 2, 5 1, 2, 5
...@@ -1035,7 +1035,7 @@ first calculation. :: ...@@ -1035,7 +1035,7 @@ first calculation. ::
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: reduce() of empty sequence with no initial value TypeError: reduce() of empty sequence with no initial value
>>> functools.reduce(operator.mul, [1,2,3], 1) >>> functools.reduce(operator.mul, [1, 2, 3], 1)
6 6
>>> functools.reduce(operator.mul, [], 1) >>> functools.reduce(operator.mul, [], 1)
1 1
...@@ -1045,9 +1045,9 @@ elements of the iterable. This case is so common that there's a special ...@@ -1045,9 +1045,9 @@ elements of the iterable. This case is so common that there's a special
built-in called :func:`sum` to compute it: built-in called :func:`sum` to compute it:
>>> import functools, operator >>> import functools, operator
>>> functools.reduce(operator.add, [1,2,3,4], 0) >>> functools.reduce(operator.add, [1, 2, 3, 4], 0)
10 10
>>> sum([1,2,3,4]) >>> sum([1, 2, 3, 4])
10 10
>>> sum([]) >>> sum([])
0 0
...@@ -1057,11 +1057,11 @@ write the obvious :keyword:`for` loop:: ...@@ -1057,11 +1057,11 @@ write the obvious :keyword:`for` loop::
import functools import functools
# Instead of: # Instead of:
product = functools.reduce(operator.mul, [1,2,3], 1) product = functools.reduce(operator.mul, [1, 2, 3], 1)
# You can write: # You can write:
product = 1 product = 1
for i in [1,2,3]: for i in [1, 2, 3]:
product *= i product *= i
A related function is :func:`itertools.accumulate(iterable, func=operator.add) A related function is :func:`itertools.accumulate(iterable, func=operator.add)
...@@ -1069,10 +1069,10 @@ A related function is :func:`itertools.accumulate(iterable, func=operator.add) ...@@ -1069,10 +1069,10 @@ A related function is :func:`itertools.accumulate(iterable, func=operator.add)
returning only the final result, :func:`accumulate` returns an iterator that returning only the final result, :func:`accumulate` returns an iterator that
also yields each partial result:: also yields each partial result::
itertools.accumulate([1,2,3,4,5]) => itertools.accumulate([1, 2, 3, 4, 5]) =>
1, 3, 6, 10, 15 1, 3, 6, 10, 15
itertools.accumulate([1,2,3,4,5], operator.mul) => itertools.accumulate([1, 2, 3, 4, 5], operator.mul) =>
1, 2, 6, 24, 120 1, 2, 6, 24, 120
...@@ -1156,7 +1156,7 @@ But it would be best of all if I had simply used a ``for`` loop:: ...@@ -1156,7 +1156,7 @@ But it would be best of all if I had simply used a ``for`` loop::
Or the :func:`sum` built-in and a generator expression:: Or the :func:`sum` built-in and a generator expression::
total = sum(b for a,b in items) total = sum(b for a, b in items)
Many uses of :func:`functools.reduce` are clearer when written as ``for`` loops. Many uses of :func:`functools.reduce` are clearer when written as ``for`` loops.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment