@@ -1010,135 +1010,6 @@ Some of the functions in this module are:
10101010Consult the operator module's documentation for a complete list.
10111011
10121012
1013-
1014- The functional module
1015- ---------------------
1016-
1017- Collin Winter's `functional module <http://oakwinter.com/code/functional/ >`__
1018- provides a number of more advanced tools for functional programming. It also
1019- reimplements several Python built-ins, trying to make them more intuitive to
1020- those used to functional programming in other languages.
1021-
1022- This section contains an introduction to some of the most important functions in
1023- ``functional ``; full documentation can be found at `the project's website
1024- <http://oakwinter.com/code/functional/documentation/> `__.
1025-
1026- ``compose(outer, inner, unpack=False) ``
1027-
1028- The ``compose() `` function implements function composition. In other words, it
1029- returns a wrapper around the ``outer `` and ``inner `` callables, such that the
1030- return value from ``inner `` is fed directly to ``outer ``. That is, ::
1031-
1032- >>> def add(a, b):
1033- ... return a + b
1034- ...
1035- >>> def double(a):
1036- ... return 2 * a
1037- ...
1038- >>> compose(double, add)(5, 6)
1039- 22
1040-
1041- is equivalent to ::
1042-
1043- >>> double(add(5, 6))
1044- 22
1045-
1046- The ``unpack `` keyword is provided to work around the fact that Python functions
1047- are not always `fully curried <http://en.wikipedia.org/wiki/Currying >`__. By
1048- default, it is expected that the ``inner `` function will return a single object
1049- and that the ``outer `` function will take a single argument. Setting the
1050- ``unpack `` argument causes ``compose `` to expect a tuple from ``inner `` which
1051- will be expanded before being passed to ``outer ``. Put simply, ::
1052-
1053- compose(f, g)(5, 6)
1054-
1055- is equivalent to::
1056-
1057- f(g(5, 6))
1058-
1059- while ::
1060-
1061- compose(f, g, unpack=True)(5, 6)
1062-
1063- is equivalent to::
1064-
1065- f(*g(5, 6))
1066-
1067- Even though ``compose() `` only accepts two functions, it's trivial to build up a
1068- version that will compose any number of functions. We'll use
1069- :func: `functools.reduce `, ``compose() `` and ``partial() `` (the last of which is
1070- provided by both ``functional `` and ``functools ``). ::
1071-
1072- from functional import compose, partial
1073- import functools
1074-
1075-
1076- multi_compose = partial(functools.reduce, compose)
1077-
1078-
1079- We can also use ``map() ``, ``compose() `` and ``partial() `` to craft a version of
1080- ``"".join(...) `` that converts its arguments to string::
1081-
1082- from functional import compose, partial
1083-
1084- join = compose("".join, partial(map, str))
1085-
1086-
1087- ``flip(func) ``
1088-
1089- ``flip() `` wraps the callable in ``func `` and causes it to receive its
1090- non-keyword arguments in reverse order. ::
1091-
1092- >>> def triple(a, b, c):
1093- ... return (a, b, c)
1094- ...
1095- >>> triple(5, 6, 7)
1096- (5, 6, 7)
1097- >>>
1098- >>> flipped_triple = flip(triple)
1099- >>> flipped_triple(5, 6, 7)
1100- (7, 6, 5)
1101-
1102- ``foldl(func, start, iterable) ``
1103-
1104- ``foldl() `` takes a binary function, a starting value (usually some kind of
1105- 'zero'), and an iterable. The function is applied to the starting value and the
1106- first element of the list, then the result of that and the second element of the
1107- list, then the result of that and the third element of the list, and so on.
1108-
1109- This means that a call such as::
1110-
1111- foldl(f, 0, [1, 2, 3])
1112-
1113- is equivalent to::
1114-
1115- f(f(f(0, 1), 2), 3)
1116-
1117-
1118- ``foldl() `` is roughly equivalent to the following recursive function::
1119-
1120- def foldl(func, start, seq):
1121- if len(seq) == 0:
1122- return start
1123-
1124- return foldl(func, func(start, seq[0]), seq[1:])
1125-
1126- Speaking of equivalence, the above ``foldl `` call can be expressed in terms of
1127- the built-in :func: `functools.reduce ` like so::
1128-
1129- import functools
1130- functools.reduce(f, [1, 2, 3], 0)
1131-
1132-
1133- We can use ``foldl() ``, ``operator.concat() `` and ``partial() `` to write a
1134- cleaner, more aesthetically-pleasing version of Python's ``"".join(...) ``
1135- idiom::
1136-
1137- from functional import foldl, partial from operator import concat
1138-
1139- join = partial(foldl, concat, "")
1140-
1141-
11421013Small functions and the lambda expression
11431014=========================================
11441015
0 commit comments