[Python-ideas] lazy list

David Mertz mertz at gnosis.cx
Fri May 15 02:11:28 CEST 2015


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:

from collections.abc import Sequence
class ExpandingSequence(Sequence):
    def __init__(self, it):
        self.it = it
        self._cache = []
    def __getitem__(self, index):
        while len(self._cache) <= index:
            self._cache.append(next(self.it))
        return self._cache[index]
    def __len__(self):
        return len(self._cache)

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.

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.

On Thu, May 14, 2015 at 7:52 AM, Alexander Atkins <alexander at tutorfair.com>
wrote:

> Hi, I'm new to this mailing list.
>
> 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
> *itertools* 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?
>
> A fuller explanation is in the README, which is here:
> https://github.com/jadatkins/python-lazylist
> 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.
>
> Warning: it may contain bugs, especially the stuff to do with slicing,
> which is not really what it's for.
>
> --
>
> *J Alexander D Atkins*
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
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/20150514/a26fdc82/attachment-0001.html>


More information about the Python-ideas mailing list