<div class="gmail_quote">On Wed, Sep 29, 2010 at 7:06 PM, Paul Rubin <span dir="ltr"><no.email@nospam.invalid></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
As for the stdlib, the natural places for such a function would be<br>
either itertools or functools, and the function should probably be called<br>
"scan", inspired by this:<br>
<br>
<a href="http://en.wikibooks.org/wiki/Haskell/List_processing#Scans" target="_blank">http://en.wikibooks.org/wiki/Haskell/List_processing#Scans</a><br>
<br>
Python's version would be like "scanl" with an optional arg to make it<br>
like "scanl1".<br></blockquote><div><br>Something like this?<br><br>NoInitialValue = object()<br><br>def scan(function, iterable, initial=NoInitialValue):<br> iterator = iter(iterable)<br><br> if initial is NoInitialValue:<br>
try:<br> accumulator = iterator.next()<br> except StopIteration:<br> return<br> else:<br> accumulator = initial<br><br> yield accumulator<br><br> for arg in iterator:<br>
accumulator = function(accumulator, arg)<br> yield accumulator<br><br>Cheers,<br>Ian <br></div></div><div style="visibility: hidden; display: inline;" id="avg_ls_inline_popup"></div><style type="text/css">#avg_ls_inline_popup { position:absolute; z-index:9999; padding: 0px 0px; margin-left: 0px; margin-top: 0px; width: 240px; overflow: hidden; word-wrap: break-word; color: black; font-size: 10px; text-align: left; line-height: 13px;}</style>