diff options
author | Guido van Rossum <guido@python.org> | 2006-08-26 20:49:04 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-08-26 20:49:04 (GMT) |
commit | 0919a1a07b061ef9a8a94cc1d92c1895b00967cb (patch) | |
tree | 812681b0b579aa36b1117b192c016b7f90ada6b0 /Doc | |
parent | 6a2a2a08329567ea41f4f073cb43e487f83872c7 (diff) | |
download | cpython-0919a1a07b061ef9a8a94cc1d92c1895b00967cb.zip cpython-0919a1a07b061ef9a8a94cc1d92c1895b00967cb.tar.gz cpython-0919a1a07b061ef9a8a94cc1d92c1895b00967cb.tar.bz2 |
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
functools, and adjust docs etc.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/howto/doanddont.tex | 14 | ||||
-rw-r--r-- | Doc/lib/libfuncs.tex | 15 | ||||
-rw-r--r-- | Doc/lib/libfunctools.tex | 13 | ||||
-rw-r--r-- | Doc/tut/tut.tex | 42 |
4 files changed, 17 insertions, 67 deletions
diff --git a/Doc/howto/doanddont.tex b/Doc/howto/doanddont.tex index a105ca1..df3ca34 100644 --- a/Doc/howto/doanddont.tex +++ b/Doc/howto/doanddont.tex @@ -289,19 +289,7 @@ There are also many useful builtin functions people seem not to be aware of for some reason: \function{min()} and \function{max()} can find the minimum/maximum of any sequence with comparable semantics, for example, yet many people write their own -\function{max()}/\function{min()}. Another highly useful function is -\function{reduce()}. A classical use of \function{reduce()} -is something like - -\begin{verbatim} -import sys, operator -nums = map(float, sys.argv[1:]) -print reduce(operator.add, nums)/len(nums) -\end{verbatim} - -This cute little script prints the average of all numbers given on the -command line. The \function{reduce()} adds up all the numbers, and -the rest is just some pre- and postprocessing. +\function{max()}/\function{min()}. On the same note, note that \function{float()}, \function{int()} and \function{long()} all accept arguments of type string, and so are diff --git a/Doc/lib/libfuncs.tex b/Doc/lib/libfuncs.tex index 65b0bf5..c9e35b5 100644 --- a/Doc/lib/libfuncs.tex +++ b/Doc/lib/libfuncs.tex @@ -836,19 +836,6 @@ class Parrot(object): \end{verbatim} \end{funcdesc} -\begin{funcdesc}{reduce}{function, sequence\optional{, initializer}} - Apply \var{function} of two arguments cumulatively to the items of - \var{sequence}, from left to right, so as to reduce the sequence to - a single value. For example, \code{reduce(lambda x, y: x+y, [1, 2, - 3, 4, 5])} calculates \code{((((1+2)+3)+4)+5)}. The left argument, - \var{x}, is the accumulated value and the right argument, \var{y}, - is the update value from the \var{sequence}. If the optional - \var{initializer} is present, it is placed before the items of the - sequence in the calculation, and serves as a default when the - sequence is empty. If \var{initializer} is not given and - \var{sequence} contains only one item, the first item is returned. -\end{funcdesc} - \begin{funcdesc}{reload}{module} Reload a previously imported \var{module}. The argument must be a module object, so it must have been successfully @@ -1058,8 +1045,6 @@ class C: The \var{sequence}'s items are normally numbers, and are not allowed to be strings. The fast, correct way to concatenate sequence of strings is by calling \code{''.join(\var{sequence})}. - Note that \code{sum(range(\var{n}), \var{m})} is equivalent to - \code{reduce(operator.add, range(\var{n}), \var{m})} \versionadded{2.3} \end{funcdesc} diff --git a/Doc/lib/libfunctools.tex b/Doc/lib/libfunctools.tex index 33a6f52..034143a 100644 --- a/Doc/lib/libfunctools.tex +++ b/Doc/lib/libfunctools.tex @@ -51,6 +51,19 @@ two: \end{verbatim} \end{funcdesc} +\begin{funcdesc}{reduce}{function, sequence\optional{, initializer}} + Apply \var{function} of two arguments cumulatively to the items of + \var{sequence}, from left to right, so as to reduce the sequence to + a single value. For example, \code{reduce(lambda x, y: x+y, [1, 2, + 3, 4, 5])} calculates \code{((((1+2)+3)+4)+5)}. The left argument, + \var{x}, is the accumulated value and the right argument, \var{y}, + is the update value from the \var{sequence}. If the optional + \var{initializer} is present, it is placed before the items of the + sequence in the calculation, and serves as a default when the + sequence is empty. If \var{initializer} is not given and + \var{sequence} contains only one item, the first item is returned. +\end{funcdesc} + \begin{funcdesc}{update_wrapper} {wrapper, wrapped\optional{, assigned}\optional{, updated}} Update a wrapper function to look like the wrapped function. The optional diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index 1b08a8e..6f6fe6f 100644 --- a/Doc/tut/tut.tex +++ b/Doc/tut/tut.tex @@ -1893,8 +1893,8 @@ use \method{pop()} with \code{0} as the index. For example: \subsection{Functional Programming Tools \label{functional}} -There are three built-in functions that are very useful when used with -lists: \function{filter()}, \function{map()}, and \function{reduce()}. +There are two built-in functions that are very useful when used with +lists: \function{filter()} and \function{map()}. \samp{filter(\var{function}, \var{sequence})} returns a sequence consisting of those items from the @@ -1934,42 +1934,6 @@ is shorter than another). For example: >>> map(add, seq, seq) [0, 2, 4, 6, 8, 10, 12, 14] \end{verbatim} - -\samp{reduce(\var{function}, \var{sequence})} returns a single value -constructed by calling the binary function \var{function} on the first two -items of the sequence, then on the result and the next item, and so -on. For example, to compute the sum of the numbers 1 through 10: - -\begin{verbatim} ->>> def add(x,y): return x+y -... ->>> reduce(add, range(1, 11)) -55 -\end{verbatim} - -If there's only one item in the sequence, its value is returned; if -the sequence is empty, an exception is raised. - -A third argument can be passed to indicate the starting value. In this -case the starting value is returned for an empty sequence, and the -function is first applied to the starting value and the first sequence -item, then to the result and the next item, and so on. For example, - -\begin{verbatim} ->>> def sum(seq): -... def add(x,y): return x+y -... return reduce(add, seq, 0) -... ->>> sum(range(1, 11)) -55 ->>> sum([]) -0 -\end{verbatim} - -Don't use this example's definition of \function{sum()}: since summing -numbers is such a common need, a built-in function -\code{sum(\var{sequence})} is already provided, and works exactly like -this. \versionadded{2.3} \subsection{List Comprehensions} @@ -2739,7 +2703,7 @@ standard module \module{__builtin__}\refbimodindex{__builtin__}: 'id', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', - 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', + 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] \end{verbatim} |