From a9091d26111552eb2214802b53ec743a75a040c8 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Thu, 22 Dec 2011 23:44:53 +0800 Subject: [PATCH] Issue13443 - Remove the functional module examples from 2.7 (as module is maintained only till 2.5 and tests had failures with 2.7) and update the links int the howto document. --- Doc/howto/functional.rst | 150 ++++----------------------------------- 1 file changed, 12 insertions(+), 138 deletions(-) diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index 4ae216a04ac..9636c6cd576 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -44,15 +44,14 @@ Programming languages support decomposing problems in several different ways: functional languages include the ML family (Standard ML, OCaml, and other variants) and Haskell. -The designers of some computer languages choose to emphasize one -particular approach to programming. This often makes it difficult to -write programs that use a different approach. Other languages are -multi-paradigm languages that support several different approaches. -Lisp, C++, and Python are multi-paradigm; you can write programs or -libraries that are largely procedural, object-oriented, or functional -in all of these languages. In a large program, different sections -might be written using different approaches; the GUI might be -object-oriented while the processing logic is procedural or +The designers of some computer languages choose to emphasize one particular +approach to programming. This often makes it difficult to write programs that +use a different approach. Other languages are multi-paradigm languages that +support several different approaches. Lisp, C++, and Python are +multi-paradigm; you can write programs or libraries that are largely +procedural, object-oriented, or functional in all of these languages. In a +large program, different sections might be written using different approaches; +the GUI might be object-oriented while the processing logic is procedural or functional, for example. In a functional program, input flows through a set of functions. Each function @@ -1115,132 +1114,6 @@ Some of the functions in this module are: Consult the operator module's documentation for a complete list. - -The functional module ---------------------- - -Collin Winter's `functional module `__ -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 -`__. - -``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 `__. 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 ``reduce()``, -``compose()`` and ``partial()`` (the last of which is provided by both -``functional`` and ``functools``). :: - - from functional import compose, partial - - multi_compose = partial(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 ``reduce`` like so:: - - 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, "") - - Revision History and Acknowledgements ===================================== @@ -1296,9 +1169,10 @@ Text Processing". Mertz also wrote a 3-part series of articles on functional programming for IBM's DeveloperWorks site; see -`part 1 `__, -`part 2 `__, and -`part 3 `__, + +`part 1 `__, +`part 2 `__, and +`part 3 `__, Python documentation