[Python-ideas] Reduce/fold and scan with generator expressions and comprehensions

David Mertz mertz at gnosis.cx
Sun Oct 23 20:12:19 EDT 2016


On Sun, Oct 23, 2016 at 4:22 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> Right. But you're missing the point of Danilo's proposal. He isn't
> asking for a function to "jump to the end" of an iterator. Look at his
> example. The word "last" is a misnomer: he seems to me talking
> about having a special variable in comprehensions that holds the
> *previous* value of the loop variable, with special syntax to set its
> FIRST value, before the loop is entered.


OK... but that's exactly itertools.accumulate (or maybe a thin wrapper
around it I like showed earlier in the thread).

I'm not sure Danilo was clear in what he's proposing.  In the thread he
suggested that he wanted to special case to indexing on sequences, which
doesn't seem to make sense for your meaning.

It feels like there might be a case here for a new function in itertools
that makes use of the last-seen item in an iterable, then combines it
somehow with the current item.  I'm not sure the spelling, but it
definitely sounds like a function to me, not a need for new syntax.  I've
only rarely had that specific need.

That said, here's a function I use in teaching to show some of what you can
do with combining iterators, especially using itertools:

def item_with_total(iterable):
    "Generically transform a stream of numbers into a pair of (num,
running_sum)"
    s, t = tee(iterable)
    yield from zip(t, accumulate(s))


This might not be *exactly* what Danilo wants, but it's a similar concept.
I wrap together an iterator (including an infinite one) with an
accumulation.  This just takes the default `operator.add` function for
accumulate(), but it could take a function argument easily enough.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161023/5712d886/attachment-0001.html>


More information about the Python-ideas mailing list