map/filter/reduce/lambda opinions and background unscientific mini-survey

Ron Adam rrr at ronadam.com
Sun Jul 3 02:06:46 EDT 2005


Steven D'Aprano wrote:

> On Sat, 02 Jul 2005 20:26:31 -0700, Devan L wrote:
> 
> 
>> Claiming that sum etc. do the same job is the whimper of
>>someone who doesn't want to openly disagree with Guido.
>>
>>Could you give an example where sum cannot do the job(besides the
>>previously mentioned product situation?
> 
> 
> There is an infinite number of potential lambdas, and therefore an
> infinite number of uses for reduce.
> 
> 
> 
> sum only handles a single case, lambda x,y: x+y
> 
> product adds a second case: lambda x,y: x*y
> 
> So sum and product together cover precisely 2/infinity, or zero percent,
> of all possible uses of reduce.

But together, sum and product, probably cover about 90% of situations in 
which you would use reduce.  Getting a total (sum) from a list probably 
covers 80% of the situations reduce would be used on it's own.  (I can't 
think of any real uses of product at the moment. It's late.)

I'm just estimating, but I think that is the gist of adding those two in 
exchange for reduce.  Not that they will replace all of reduce use 
cases, but that sum and product cover most situations and can be 
implemented more efficiently than using reduce or a for loop to do the 
same thing.  The other situations can easily be done using for loops, so 
it's really not much of a loss.

Ron



More information about the Python-list mailing list