mirror of
https://github.com/python/cpython.git
synced 2025-07-30 22:54:16 +00:00
The functional module hasn't been maintained since 2006 and doesn't work with Python 3.
Remove section about it from the functional programming FAQ.
This commit is contained in:
commit
b53e2c4900
1 changed files with 0 additions and 129 deletions
|
@ -1010,135 +1010,6 @@ Some of the functions in this module are:
|
||||||
Consult the operator module's documentation for a complete list.
|
Consult the operator module's documentation for a complete list.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The functional module
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
Collin Winter's `functional module <http://oakwinter.com/code/functional/>`__
|
|
||||||
provides a number of more advanced tools for functional programming. It also
|
|
||||||
reimplements several Python built-ins, trying to make them more intuitive to
|
|
||||||
those used to functional programming in other languages.
|
|
||||||
|
|
||||||
This section contains an introduction to some of the most important functions in
|
|
||||||
``functional``; full documentation can be found at `the project's website
|
|
||||||
<http://oakwinter.com/code/functional/documentation/>`__.
|
|
||||||
|
|
||||||
``compose(outer, inner, unpack=False)``
|
|
||||||
|
|
||||||
The ``compose()`` function implements function composition. In other words, it
|
|
||||||
returns a wrapper around the ``outer`` and ``inner`` callables, such that the
|
|
||||||
return value from ``inner`` is fed directly to ``outer``. That is, ::
|
|
||||||
|
|
||||||
>>> def add(a, b):
|
|
||||||
... return a + b
|
|
||||||
...
|
|
||||||
>>> def double(a):
|
|
||||||
... return 2 * a
|
|
||||||
...
|
|
||||||
>>> compose(double, add)(5, 6)
|
|
||||||
22
|
|
||||||
|
|
||||||
is equivalent to ::
|
|
||||||
|
|
||||||
>>> double(add(5, 6))
|
|
||||||
22
|
|
||||||
|
|
||||||
The ``unpack`` keyword is provided to work around the fact that Python functions
|
|
||||||
are not always `fully curried <http://en.wikipedia.org/wiki/Currying>`__. By
|
|
||||||
default, it is expected that the ``inner`` function will return a single object
|
|
||||||
and that the ``outer`` function will take a single argument. Setting the
|
|
||||||
``unpack`` argument causes ``compose`` to expect a tuple from ``inner`` which
|
|
||||||
will be expanded before being passed to ``outer``. Put simply, ::
|
|
||||||
|
|
||||||
compose(f, g)(5, 6)
|
|
||||||
|
|
||||||
is equivalent to::
|
|
||||||
|
|
||||||
f(g(5, 6))
|
|
||||||
|
|
||||||
while ::
|
|
||||||
|
|
||||||
compose(f, g, unpack=True)(5, 6)
|
|
||||||
|
|
||||||
is equivalent to::
|
|
||||||
|
|
||||||
f(*g(5, 6))
|
|
||||||
|
|
||||||
Even though ``compose()`` only accepts two functions, it's trivial to build up a
|
|
||||||
version that will compose any number of functions. We'll use
|
|
||||||
:func:`functools.reduce`, ``compose()`` and ``partial()`` (the last of which is
|
|
||||||
provided by both ``functional`` and ``functools``). ::
|
|
||||||
|
|
||||||
from functional import compose, partial
|
|
||||||
import functools
|
|
||||||
|
|
||||||
|
|
||||||
multi_compose = partial(functools.reduce, compose)
|
|
||||||
|
|
||||||
|
|
||||||
We can also use ``map()``, ``compose()`` and ``partial()`` to craft a version of
|
|
||||||
``"".join(...)`` that converts its arguments to string::
|
|
||||||
|
|
||||||
from functional import compose, partial
|
|
||||||
|
|
||||||
join = compose("".join, partial(map, str))
|
|
||||||
|
|
||||||
|
|
||||||
``flip(func)``
|
|
||||||
|
|
||||||
``flip()`` wraps the callable in ``func`` and causes it to receive its
|
|
||||||
non-keyword arguments in reverse order. ::
|
|
||||||
|
|
||||||
>>> def triple(a, b, c):
|
|
||||||
... return (a, b, c)
|
|
||||||
...
|
|
||||||
>>> triple(5, 6, 7)
|
|
||||||
(5, 6, 7)
|
|
||||||
>>>
|
|
||||||
>>> flipped_triple = flip(triple)
|
|
||||||
>>> flipped_triple(5, 6, 7)
|
|
||||||
(7, 6, 5)
|
|
||||||
|
|
||||||
``foldl(func, start, iterable)``
|
|
||||||
|
|
||||||
``foldl()`` takes a binary function, a starting value (usually some kind of
|
|
||||||
'zero'), and an iterable. The function is applied to the starting value and the
|
|
||||||
first element of the list, then the result of that and the second element of the
|
|
||||||
list, then the result of that and the third element of the list, and so on.
|
|
||||||
|
|
||||||
This means that a call such as::
|
|
||||||
|
|
||||||
foldl(f, 0, [1, 2, 3])
|
|
||||||
|
|
||||||
is equivalent to::
|
|
||||||
|
|
||||||
f(f(f(0, 1), 2), 3)
|
|
||||||
|
|
||||||
|
|
||||||
``foldl()`` is roughly equivalent to the following recursive function::
|
|
||||||
|
|
||||||
def foldl(func, start, seq):
|
|
||||||
if len(seq) == 0:
|
|
||||||
return start
|
|
||||||
|
|
||||||
return foldl(func, func(start, seq[0]), seq[1:])
|
|
||||||
|
|
||||||
Speaking of equivalence, the above ``foldl`` call can be expressed in terms of
|
|
||||||
the built-in :func:`functools.reduce` like so::
|
|
||||||
|
|
||||||
import functools
|
|
||||||
functools.reduce(f, [1, 2, 3], 0)
|
|
||||||
|
|
||||||
|
|
||||||
We can use ``foldl()``, ``operator.concat()`` and ``partial()`` to write a
|
|
||||||
cleaner, more aesthetically-pleasing version of Python's ``"".join(...)``
|
|
||||||
idiom::
|
|
||||||
|
|
||||||
from functional import foldl, partial from operator import concat
|
|
||||||
|
|
||||||
join = partial(foldl, concat, "")
|
|
||||||
|
|
||||||
|
|
||||||
Small functions and the lambda expression
|
Small functions and the lambda expression
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue