[Python-ideas] Start argument for itertools.accumulate() [Was: Proposal: A Reduce-Map Comprehension and a "last" builtin]
Raymond Hettinger
raymond.hettinger at gmail.com
Sat Apr 7 03:44:37 EDT 2018
> On Apr 6, 2018, at 9:06 PM, Tim Peters <tim.peters at gmail.com> wrote:
>
>>
>> What is this code trying to accomplish?
>
> It's quite obviously trying to bias the reader against the proposal by
> presenting a senseless example ;-)
FWIW, the example was not from me. It was provided by the OP on the tracker. I changed the start point from 10 to a 6 so it at least made some sense as the continuation of a factorial sequence: 6 24 120
> By sheer coincidence, I happened to write another yesterday. This is
> from a program looking for the smallest integers that yield new
> records for Collatz sequence lengths.
Nice. That brings the number of real-world examples up to a total of three (collatz, wheel sieve, and signal processing). Prior to today, that total was only one (which was found after much digging).
> Later:
>
> def coll(SHIFT=24):
> ...
> from itertools import accumulate, chain, cycle
> ...
> LIMIT = 1 << SHIFT
> ...
> abc, first, deltas = buildtab(SHIFT, LIMIT)
> ...
> for num in accumulate(chain([first], cycle(deltas))):
> assert num % 3 != 2
>
> As in Will's code, it would be more readable as:
>
> for num in accumulate(cycle(deltas), start=first):
That does read better. I am curious how you would have written it as a plain for-loop before accumulate() was added (part of the argument against reduce() was that a plain for-loop would be clearer 99% of the time).
> That said, if the need came up often, as you noted it's dead easy to
> write a helper function to encapsulate the "head scratcher" part, and
> with no significant loss of efficiency.
>
> So I'd be -0 overall, _except_ that "chain together a singleton list
> and a cycle" is so obscure on the face of it than I'm not sure most
> programmers who wanted the functionality of `start=` would ever think
> of it. I'm not sure that I would have, except that I studied Ness's
> wheel sieve code a long time ago and the idea stuck. So that makes me
> +0.4.
Agreed that the "chain([x], it)" step is obscure. That's a bit of a bummer -- one of the goals for the itertools module was to be a generic toolkit for chopping-up, modifying, and splicing iterator streams (sort of a CRISPR for iterators). The docs probably need another recipe to show this pattern:
def prepend(value, iterator):
"prepend(1, [2, 3, 4]) -> 1 2 3 4"
return chain([value], iterator)
Thanks for taking a look at the proposal. I was -0 when it came up once before. Once I saw a use case pop-up on this list, I thought it might be worth discussing again.
Raymond
More information about the Python-ideas
mailing list