Idea for reduce (Re: Question regarding a recent article on informit.com)

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Sun Mar 23 23:43:40 EST 2003


Beni Cherniavsky wrote:
> ``sub up`` wouldn't look good :-).

It wouldn't, although I don't think I'd want to use
sub in a reduction anyway -- the concept really only
makes sense for associative operators, so out of the
operator module that would mean

   x = add up y
   x = mul up y
   x = and_ up y
   x = or_ up y
   x = xor up y
   x = concat up y

Some of them sound a bit strange, I admit, but once
you get the idea, they make sense in a funky kind of
way...

> You also need syntax for initial value.

   x = add up numbers from 0

I would allow the initial value to be omitted when
you know the list won't be empty (the first item in
the list would be the initial value in that case).

Alternatively there could be a protocol which gets
the initial value from an attribute of the callable.

>     total = (accumulate sum = 0 then sum + n for n in numbers)
> 
> I'd vote this YAGNI since the equivallent loop is more readable
> anyway::
> 
>     total = 0
>     for n in numbers:
>         total += n

Indeed, it's very hard to come up with anything that
has all the required degrees of freedom and is
significantly more concise than just writing it out.

So, until a sufficiently brilliant solution occurs
to me, I'll vote IAWTB (It Ain't Worth The Bother).

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list