Bill Janssen wrote:
>>> It should amount to "map(+, operands)".
>> Or, to be pedantic, this:
>>
>>      reduce(lambda x, y: x.__add__(y), operands)
> 
> Don't you mean:
> 
>    reduce(lambda x, y:  x.__add__(y), operands[1:], operands[0])

This is a nice illustration of a fairly significant issue with the 
usability of reduce: two attempts to rewrite sum() using reduce(), and 
both of them are buggy. Neither of the solutions above can correctly 
handle an empty sequence:

.>>> operands = []
.>>> reduce(lambda x, y: x.__add__(y), operands).
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
.>>> reduce(lambda x, y:  x.__add__(y), operands[1:], operands[0])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
IndexError: list index out of range

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to