Algorithms on iterators (consuming vs buffering) #1617
dabrahams
started this conversation in
Language design
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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