<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Oct 23, 2016 at 4:22 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Right. But you're missing the point of Danilo's proposal. He isn't<br>
asking for a function to "jump to the end" of an iterator. Look at his<br>
example. The word "last" is a misnomer: he seems to me talking<br>
about having a special variable in comprehensions that holds the<br>
*previous* value of the loop variable, with special syntax to set its<br>
FIRST value, before the loop is entered. </blockquote><div><br></div><div>OK... but that's exactly itertools.accumulate (or maybe a thin wrapper around it I like showed earlier in the thread). </div><div><br></div><div>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.</div><div><br></div><div>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.</div></div><div class="gmail_extra"><br></div><div class="gmail_extra">That said, here's a function I use in teaching to show some of what you can do with combining iterators, especially using itertools:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:0px;padding:0px;font-size:14px;color:rgb(51,51,51);border-radius:4px;line-height:inherit;word-break:break-all;word-wrap:break-word;background-color:rgb(247,247,247);border:none"><span class="gmail-m_-6536918666925219276gmail-k" style="margin:0px;padding:0px;color:rgb(0,128,0);font-weight:bold">def</span> <span class="gmail-m_-6536918666925219276gmail-nf" style="margin:0px;padding:0px;color:blue">item_with_total</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">(</span><span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">iterable</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">):</span>
<span class="gmail-m_-6536918666925219276gmail-s" style="margin:0px;padding:0px;color:rgb(186,33,33)">"Generically transform a stream of numbers into a pair of (num, running_sum)"</span>
<span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">s</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">,</span> <span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">t</span> <span class="gmail-m_-6536918666925219276gmail-o" style="margin:0px;padding:0px;color:rgb(102,102,102)">=</span> <span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">tee</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">(</span><span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">iterable</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">)</span>
<span class="gmail-m_-6536918666925219276gmail-k" style="margin:0px;padding:0px;color:rgb(0,128,0);font-weight:bold">yield from</span> <span class="gmail-m_-6536918666925219276gmail-nb" style="margin:0px;padding:0px;color:green">zip</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">(</span><span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">t</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">,</span> <span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">accumulate</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">(</span><span class="gmail-m_-6536918666925219276gmail-n" style="margin:0px;padding:0px">s</span><span class="gmail-m_-6536918666925219276gmail-p" style="margin:0px;padding:0px">))</span></pre></div><div class="gmail_extra"><br></div>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.<br clear="all"><div><br></div>-- <br><div class="gmail-m_1055424009678975389gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>