I think I understand now. <div>Still, for the more general case, I think it could be useful to be able to specify a max window size for the the dequeue (or indexable list-like structure);</div><div>though defaulting to None and storing all values may be additionally useful. <br><br>On Monday, November 14, 2016, Danilo J. S. Bellini <<a href="mailto:danilo.bellini@gmail.com">danilo.bellini@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-11-06 23:27 GMT-02:00 Wes Turner <span dir="ltr"><<a href="javascript:_e(%7B%7D,'cvml','wes.turner@gmail.com');" target="_blank">wes.turner@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">- So, IIUC, for recursive list comprehensions<div>  - "prev" = x_(n-1)</div><div>  - there is a need to define an initial value</div><div>    - chain([1000], [...])</div><div>  - sometimes, we actually need window function</div><div>    - __[0] = x_(n-1)</div><div>    - __[1] = x_(n-2)  # this</div><div>    - __[-1] = x_(n-2)  # or this</div><div>    - this can be accomplished with dequeue</div><div>      - __= dequeue([1000], maxlen)</div><div>    - for recursive list comprehensions, we'd want to bind e.g. __ to a dequeue</div><div><br></div><div>[f(__[0], x) for x in y with __ = dequeue((1000,), 1)]</div></blockquote></div><br>If I understood correctly, that's an alternative to get a general recursive list comprehension with a syntax like:<br><br><span style="font-family:monospace,monospace">[f(hist, x) for x in y with hist = deque([start_values], size)]</span><br><br></div><div class="gmail_extra">You're trying to solve the output lag/window problem using a circular queue with random/indexed access to its values (a <span style="font-family:monospace,monospace">collections.deque</span> instance). You're using "<span style="font-family:monospace,monospace">with</span>" instead of "<span style="font-family:monospace,monospace">from</span>" to distinguish it from my first proposal.<br><br></div><div class="gmail_extra">That's not a scan anymore, but something more general. Some information can be removed from that syntax, for example the size can be defined to be the starting iterable/memory/history data size, and the deque can be something internal. Also, using the negative indices would be more explicit as <span style="font-family:monospace,monospace">hist[-1]</span> would be the previous iteration result, <span style="font-family:monospace,monospace">hist[-2]</span> would be its former result and so on. The syntax would be:<br><br></div><div class="gmail_extra"><span style="font-family:monospace,monospace">[func(hist, target) for target in iterable with hist = start_iterable]</span><br><br></div><div class="gmail_extra">i.e., this idea is about a new "<span style="font-family:monospace,monospace">with hist = start_iterable</span>" at the end (or "<span style="font-family:monospace,monospace">from</span>" instead of "<span style="font-family:monospace,monospace">with</span>"). The resulting list size would be <span style="font-family:monospace,monospace">len(list(start_iterable)) + len(list(iterable))</span>. As a generator instead of a list, that can be implemented as this "windowed scan" generator function:<br></div><div class="gmail_extra"><br><span style="font-family:monospace,monospace">>>> import collections<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">>>> def wscan(func, iterable, start_iterable):<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...     pre_hist = []<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...     for item in start_iterable:<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...         yield item<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...         pre_hist.append(item)<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...     hist = collections.deque(pre_hist, len(pre_hist))<br></span><div class="gmail_extra"><span style="font-family:monospace,monospace">...     for target in iterable:<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...         item = func(hist, target)</span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...         yield item<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...         hist.append(item)</span><br></div><br></div><div class="gmail_extra">The Fibonacci example would be written as:<br></div><div class="gmail_extra"><br><span style="font-family:monospace,monospace">>>> list(wscan(lambda fibs, unused: fibs[-1] + fibs[-2], range(10), [0, 1]))<br>[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]<br></span><br></div><div class="gmail_extra">With the "windowed scan" syntax proposal, it would become:<br></div><div class="gmail_extra"><br></div><div class="gmail_extra"><span style="font-family:monospace,monospace">>>> [fibs[-1] + fibs[-2] for unused in range(10) with fibs = [0, 1]]<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]<br></span><br></div><div class="gmail_extra">Or:<br><br></div><div class="gmail_extra"><div class="gmail_extra"><span style="font-family:monospace,monospace">>>> [fibs[-1] + fibs[-2] for unused in range(10) from fibs = [0, 1]]<br></span></div><span style="font-family:monospace,monospace">[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]<br></span><br>-- <br><div>Danilo J. S. Bellini<br>---------------<br>"<i>It is not our business to set up prohibitions, but to arrive at conventions.</i>" (R. Carnap)<br></div>
</div></div>
</blockquote></div>