Tim Peters wrote:
while we have N numbers, there are N+1 slice indices.  So
accumulate(xs) doesn't quite work.  It needs to also have a 0 inserted
as the first prefix sum (the empty prefix sum(xs[:0]).

Which is exactly what a this_is_the_initial_value=0 argument would do
for us.

In this case, yes. But that still doesn't mean it makes
sense to require the initial value to be passed *in* as
part of the input sequence.

Maybe the best idea is for the initial value to be a
separate argument, but be returned as the first item in
the list.

I can think of another example where this would make
sense. Suppose you have an initial bank balance and a
list of transactions, and you want to produce a statement
with a list of running balances.

The initial balance and the list of transactions are
coming from different places, so the most natural way
to call it would be

   result = accumulate(transactions, initial = initial_balance)

If the initial value is returned as item 0, then the
result has the following properties:

   result[0] is the balance brought forward
   result[-1] is the current balance

and this remains true in the corner case where there are
no transactions.

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to