On Fri, Jun 22, 2007 at 07:11:14PM +1000, Nick Coghlan wrote: > 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
Maybe the specification/documentation is missing some phrasing like that : "The function must also be able to accept no arguments." (taken from another language spec.) ? Better fix the documentation than blame reduce. Of course, reduce was taken from Lisp, where lambda is not castrated and thus allows one to write the no-argument case with more ease. Castrated lambdas limit the usefulness of reduce *in python*, not in general. Regards, Auélien. > 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/aurelien.campeas%40logilab.fr _______________________________________________ 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
