reduce()--what is it good for? (was: Re: reduce() anomaly?)
Erik Max Francis
max at alcyone.com
Wed Nov 5 23:58:41 CET 2003
Francis Avila wrote:
> Just out of curiosity, for what kind of problems do we find reduce to
> be the Right Way? I mean, summing a big list of numbers is fun and
> all, but
> I never find any use for it otherwise. I *often* try to think if
> would be useful when I come across some collection-of-values
> task, but usually one wants to repeat an operation on a whole set of
> not reduce the set to one value.
I don't use reduce extremely routinely, but I certainly do find myself
using it. Grepping through my Python sources, the most common uses I
find are summing together lists of numeric values and joining together
(flattening) a list of lists (though only when the contained lists
themselves do not contain any sequences as elements).
> So, are there any *non-trivial* and *unabusive* uses of reduce, where
> other way would be tedious, repetitive, slow, unclear, etc.? I'm very
> curious to see them.
My cellular automaton engine CAGE uses reduce several times, although
admittedly this use is academic (obviously a good way to implement a
ReductionRule is almost by definition with a reduce operation :-). Even
then, there are times when, say, you want to get the sum of the states
of the cells in the neighborhood, and "neighborhood" is defined in a
sufficiently generic way that the states of the neighborhood are just a
list of state values.
My Solar System calculator BOTEC has a similar application; when
plotting courses, you can have an arbitrary number of transfers, and
those transfers can each have an arbitrary number of burns. So it's
quite convenient to do a reduce on the list of burns (a sequence of
sequences), and then a reduce on the list of deltavees for the transfers
(a sequence of numerics).
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Thousands have lived without love, not one without water.
\__/ W.H. Auden
More information about the Python-list