
On Sat, Jul 23, 2016 at 3:58 AM, David Mertz <mertz@gnosis.cx> wrote:
What you might be looking at is a protocol for "bookmarking" or "forking" an iterator. That might be more useful. For example:
How is this different from itertools.tee()?
Functionally? Not at all. In fact, itertools.tee could make use of the fork protocol. The idea would be efficiency; iterators that are "restartable" by your definition would be "forkable", which would effectively permit tee to just ask the iterator to tee itself. The simple and naive implementation of tee() is "save all the elements until they get asked for", which requires extra storage for those elements; if your iterator can fork, that implies that you can get those elements right back again from the original. Imagine a list_iterator consists of two things, a base list and a current index. Tee'ing that iterator means retrieving elements and hanging onto them until needed; forking means creating another iterator with the same index. Which means that a forked iterator would reflect changes to the original list, for better or for worse. Not saying it's necessarily a good thing, but it's a more general version of the concept of "restarting" an iterator, being more compatible with trimmed iterators and such. Ultimately, your "restart" is no different from itertools.tee either. ChrisA