[Python-ideas] lazy list
Steven D'Aprano
steve at pearwood.info
Thu May 14 17:24:33 CEST 2015
On Thu, May 14, 2015 at 03:52:55PM +0100, Alexander Atkins 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
Why? It's not really an iterator tool. The things in itertools are tools
for processing streams, not containers.
> 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.
What sort of uses? Can you give some examples?
I'm having trouble thinking of a situation where I might use something
like that. If I want random access, I'd use a list, or a computed
sequence like (x)range. I don't think I would want something which acts
like a generator but quietly holds onto all the items it has seen
before, whether I need them or not.
> Warning: it may contain bugs, especially the stuff to do with slicing,
> which is not really what it's for.
A slice is just a subsequence of indexed values. If you can index it,
you should be able to slice it.
assert spam[start:end:step] == [spam[i] for i in range(start, end, step)]
--
Steve
More information about the Python-ideas
mailing list