Algorithms on iterators (consuming vs buffering) #1617
Replies: 1 comment
-
It may still be useful to have that kind of iterator with the buffer separate from the one that simply presents a collection. One issue to consider is that if you give me a At the same time, I am partial to the simpler approach of saying that Perhaps one way to refine our opinion could be to think of how we want to use these iterators. It seems to me that the most common use case will be consuming iteration, in which case any algorithm that may exhaust its input could sink it. |
Beta Was this translation helpful? Give feedback.
-
This is probably just a tangent in a larger conversation we're having about iteration protocols, by which I mean we shouldn't get too far into the larger topic in this discussion.
Supposing you have
We could write an algorithm for Iterator that tells whether two iterators have equal elements:
The semantics of where it leaves self and other are a bit hard to describe:
[(x) means not only is the iterator exhausted, but it has returned
nil
fromnext()
. That would only be significant if don't require iterators to keep returningnil
once exhausted. I think we should require that, especially looking at these results.]At least it looks symmetrical!
The rule seems to be,
I don't know whether that semantics is useful or not. Certainly if not, such an algorithm should be made to
sink
both iterators in question. One alternative I don't love is to simplify the semantics by leaving the middle case unspecified, and only giving guarantees in the other two cases.We could have a kind of iterator that could buffer its last element, so we could check for exhaustion without consuming an element. That would allow us to create a much more useful semantics, stopping the iterators in a place where the first mismatch could be examined, and always consuming the same number of elements from both iterators. However, IIRC we discussed schemes like this and decided that once you require buffering, you may as well require multipass, and it wasn't worth complicating the design space by separating the two aspects. Please correct me if I'm wrong.
Beta Was this translation helpful? Give feedback.
All reactions