<div dir="ltr">I actually taught almost exactly this two days ago as an example of a class in the context of laziness, and included it in a white paper I wrote for O'Reilly on _Functional Programming in Python_ that will be given out starting at OSCon. I'm sure I'm also not the first or the 50th person to think of it. My basic implementation--made to exhibit a concept not to be complete--was rather short:<div><br></div><div><div>from collections.abc import Sequence</div><div>class ExpandingSequence(Sequence):</div><div> def __init__(self, it):</div><div> <a href="http://self.it">self.it</a> = it</div><div> self._cache = []</div><div> def __getitem__(self, index):</div><div> while len(self._cache) <= index:</div><div> self._cache.append(next(<a href="http://self.it">self.it</a>))</div><div> return self._cache[index]</div><div> def __len__(self):</div><div> return len(self._cache)</div></div><div><br></div><div>I think it's kinda cute. Especially when passed in something like an infinite iterator of all the primes or all the Fibonacci numbers. But I can't really recommend it (nor the fleshed out version the OP wrote) for the standard library. There's no size limit to the object, and so we don't *really* save space over just appending more elements to a list. </div><div><br></div><div>I can certainly see that it could be OK for particular people with particular use cases, but it doesn't feel general enough for stdlib.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 14, 2015 at 7:52 AM, Alexander Atkins <span dir="ltr"><<a href="mailto:alexander@tutorfair.com" target="_blank">alexander@tutorfair.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, I'm new to this mailing list.</div><div><br></div><div>I needed a lazy list implementation for something, so I created one. I was a little bit surprised to find that there wasn't one in the <b>itertools</b> module and it seemed like quite a basic thing to me, as someone who has used Haskell before, so I thought probably I should share it. I'm wondering whether something like this should be part of the standard library?</div><div><br></div><div>A fuller explanation is in the README, which is here: <a href="https://github.com/jadatkins/python-lazylist" target="_blank">https://github.com/jadatkins/python-lazylist</a></div><div>The gist of it is that it allows you to index into a generator. Previously evaluated elements are remembered, so foo[5] returns the same thing each time, and you can later call foo[4] and get the previous element. There are many uses for such a thing, but if you're not expecting it in the language, you might not necessarily think of them.</div><div><br></div><div>Warning: it may contain bugs, especially the stuff to do with slicing, which is not really what it's for.</div><br clear="all"><div><div><div dir="ltr"><div dir="ltr"><span style="font-family:'Times New Roman';font-size:12.8px;color:rgb(102,102,102);display:table-cell;vertical-align:middle"><div dir="ltr" style="color:rgb(0,0,0);font-size:medium"><span style="font-size:12.8px;color:rgb(102,102,102)"><font face="arial, helvetica, sans-serif">--</font></span></div><div dir="ltr" style="color:rgb(0,0,0);font-size:medium"><span style="font-size:12.8px;color:rgb(102,102,102)"><b><font face="arial, helvetica, sans-serif">J Alexander D Atkins<br></font></b></span></div><div dir="ltr" style="color:rgb(0,0,0);font-size:medium"></div></span><span style="font-family:'Times New Roman';font-size:12.8px;color:rgb(102,102,102);display:table-cell;vertical-align:middle"><font face="arial, helvetica, sans-serif"><br></font></span><div><div dir="ltr"></div></div></div></div></div></div>
</div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_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>