From: Paul Rubin on
Brian Victor <homeusenet4(a)brianhv.org> writes:
> def running_sum(result, current_value):
> return result + [result[-1]+current_value if result else current_value]
>
> reduce(running_sum, x, [])

That is not really any good because Python lists are actually vectors,
so result+[...] actually copies the whole old list, making your function
take quadratic time. It would be ok in a FP language where lists were
chains of cons nodes and result+[...] just allocated a single cons.

I think Peter Otten's solution involving a generator is the one most in
the current Python spirit. It's cleaner (for my tastes) than the ones
that use things like list.append.
From: Joel Goldstick on
Peter Otten wrote:
> dhruvbird wrote:
>
>> I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ]
>> And would like to compute the cumulative sum of all the integers
>> from index zero into another array. So for the array above, I should
>> get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ]
>> What is the best way (or pythonic way) to get this.
>
> Homework?
>
>>>> def cumulative_sum(values, start=0):
> ... for v in values:
> ... start += v
> ... yield start
> ...
>>>> list(cumulative_sum([ 0, 1, 2, 1, 1, 0, 0, 2, 3 ]))
> [0, 1, 3, 4, 5, 5, 5, 7, 10]
>
> Peter

nice! Peter

From: John Nagle on
On 7/19/2010 9:56 AM, dhruvbird wrote:
> On Jul 19, 9:12 pm, Brian Victor<homeusen...(a)brianhv.org> wrote:
>> dhruvbird wrote:

>> Having offered this, I don't recall ever seeing reduce used in real
>> python code, and explicit iteration is almost always preferred.
>
> Yes, even I have noticed that reduce is a tad under-used function.

Yes, I had a use case for it once, but it wasn't worth the trouble.
"map" is often useful, but "reduce", not so much.

Python isn't really a functional language. There's no bias toward
functional solutions, lambdas aren't very general, and the performance
isn't any better. Nor is any concurrency provided by "map" or "reduce".
So there's no win in trying to develop cute one-liners.

John Nagle
From: dhruvbird on
On Jul 21, 8:17 pm, John Nagle <na...(a)animats.com> wrote:
> On 7/19/2010 9:56 AM, dhruvbird wrote:
>
> > On Jul 19, 9:12 pm, Brian Victor<homeusen...(a)brianhv.org>  wrote:
> >> dhruvbird wrote:
> >> Having offered this, I don't recall ever seeing reduce used in real
> >> python code, and explicit iteration is almost always preferred.
>
> > Yes, even I have noticed that reduce is a tad under-used function.
>
>      Yes, I had a use case for it once, but it wasn't worth the trouble.
> "map" is often useful, but "reduce", not so much.
>
>      Python isn't really a functional language.  There's no bias toward
> functional solutions, lambdas aren't very general, and the performance
> isn't any better.  Nor is any concurrency provided by "map" or "reduce"..
> So there's no win in trying to develop cute one-liners.

Yes agreed.

However, there is:

1. now scope for optimization (for example returning generators
instead of lists) at every stage if using functions -- these functions
can be internally changed as long as the external guarantees they
provide remain essentially unchanged.

2. readability wins because you express your intent (operations)
rather than anything else.
For example, if I want the product of the square roots of all odd
integers in an array, I can say:
answer = reduce(product, map(math.sqrt, filter(lambda x: x%2 == 0,
some_array_with_ints)))

While I agree that python may not have been initially seen as a
functional language, it is powerful and flexible enough to be one or
at least decently support such paradigms.

Regards,
-Dhruv.
From: geremy condra on
On Wed, Jul 21, 2010 at 8:17 AM, John Nagle <nagle(a)animats.com> wrote:
> On 7/19/2010 9:56 AM, dhruvbird wrote:
>>
>> On Jul 19, 9:12 pm, Brian Victor<homeusen...(a)brianhv.org>  wrote:
>>>
>>> dhruvbird wrote:
>
>>> Having offered this, I don't recall ever seeing reduce used in real
>>> python code, and explicit iteration is almost always preferred.
>>
>> Yes, even I have noticed that reduce is a tad under-used function.
>
>    Yes, I had a use case for it once, but it wasn't worth the trouble..
> "map" is often useful, but "reduce", not so much.
>
>    Python isn't really a functional language.  There's no bias toward
> functional solutions, lambdas aren't very general, and the performance
> isn't any better.  Nor is any concurrency provided by "map" or "reduce"..
> So there's no win in trying to develop cute one-liners.

Too bad about the lack of concurrency, would be many places where that
would be nice.

Geremy Condra