Python Tutorial Was: Guido's regrets: filter and map

Anders J. Munch andersjm at dancontrol.dk
Wed Nov 27 04:49:02 EST 2002


"Lulu of the Lotus-Eaters" <mertz at gnosis.cx>:
> ||And it doesn't replace reduce at all -- we've yet to see a construct
> ||proposed to do that (aside from writing it out as an explicit loop).
> 
> Actually... here's an... ummm... "improved" version:
> 
> reduce = lambda func, seq, init=None:\
>          [(l.append(func(l[i],seq[i])),l[-1])
>                          for l in [[init or seq[0]]]
>                          for i in range(len(seq))][-1][1]
> 
> Yours, Lulu...

Needs more "improvement" ;-)

>>> reduce(operator.add, [1,2,3])
6
>>> lulu_reduce(operator.add, [1,2,3])
7

>>>def yieldall(seq):
>>>    for x in seq: yield x
>>> reduce(operator.add, yieldall([1,2,3]))
6
>>>lulu_reduce(operator.add, yieldall([1,2,3]))
TypeError: unsubscriptable object

- Anders





More information about the Python-list mailing list