[issue25193] itertools.accumulate should have an optional initializer argument

Mark Dickinson report at bugs.python.org
Mon Sep 21 14:09:07 CEST 2015


Mark Dickinson added the comment:

A couple of observations:

1. Haskell's (rough) equivalents would be `mapAccumL` and `mapAccumR`, which do take an initializer [1].  The signature for both functions is something like:

(acc -> b -> (acc, c)) -> acc -> [b] -> (acc, [c]).

2. In the particular case of NumPy, I've found np.cumsum a pain to use on multiple occasions because of the missing left-hand point in the result.  Grepping through a couple of recent (real-world) projects produces code like:

    summed_widths = cumsum(hstack(([0], widths[:-1])))

and

    cumulative_segments = np.insert(np.cumsum(segment_lengths), 0, 0.0)

where that extra missing value is having to be inserted either in the input list or the output list. OTOH, none of those projects is natural fit for itertools, so changing itertools wouldn't help them directly.

[1] https://www.haskell.org/hoogle/?hoogle=mapAccumL

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25193>
_______________________________________


More information about the Python-bugs-list mailing list