Nicer way to write folding? (list reversal?)

Michael Hudson mwh at python.net
Thu Jan 3 05:59:50 EST 2002


andrew at acooke.org writes:

> Hi,
> 
> In the code below, foldr2 is clearly uglier than the rest.  Is there a
> nicer way (without destructive reversal - yuck - of the list
> argument)?

No.  It's not that bad is it?

>  Also, are these things built-in (can't find them)?

Well, there's reduce, but it's almost always a bad idea.

>  Is recursion any faster than it used to be 

Nope.

> (I've not been following Python development so am wondering if
> anything in 2 (or 2.2) would help;

Oh, if you're migrating from 1.5.2 it might be a touch quicker.
Nothing really siginificant though.

> foldr2 and fold2 are expected to be faster than foldl and
> foldr, although I've not done any timing).

I'd be amazed if they weren't.

Now for some content <wink>: why are you writing these functions?
It's not really a sensible way to program in Python.

Cheers,
M.



More information about the Python-list mailing list