Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse iteration protocol #23972

Closed
stevengj opened this issue Oct 2, 2017 · 6 comments · Fixed by #24187
Closed

reverse iteration protocol #23972

stevengj opened this issue Oct 2, 2017 · 6 comments · Fixed by #24187
Labels
collections Data structures holding multiple items, e.g. sets design Design of APIs or of the language itself

Comments

@stevengj
Copy link
Member

stevengj commented Oct 2, 2017

As has been mentioned a few times, e.g. in #4590, #6165, and #23969, it would be nice if there were a standard reverse iteration protocol that iterators could support (if possible). @StefanKarpinski suggested rstart, prev, and rdone as not-terrible possibilities.

You could then do e.g. for x in Reverse(itr) and Reverse (unlike reverse) would implement non-materializing reverse iteration by calling rstart etc. on itr.

@stevengj stevengj added collections Data structures holding multiple items, e.g. sets design Design of APIs or of the language itself labels Oct 2, 2017
@Keno
Copy link
Member

Keno commented Oct 2, 2017

Why not just have Reverse be a wrapper type and dispatch on start(::Reverse{...}).

@oscardssmith
Copy link
Member

If Reverse would it just error on infinite iterators?

@stevengj
Copy link
Member Author

stevengj commented Oct 2, 2017

@oscardssmith, yes, you'd get an error (probably MethodError) for any iterator where reverse iteration is impossible/impractical/unimplemented.

@malmaud
Copy link
Contributor

malmaud commented Oct 3, 2017

I favor Keno's solution, since it also scales to different kinds of iterator orders. e.g, what if an iterator has efficient support for only iterating the even elements? Do we then need evenstart in Base?

Also, I don't remember the issue, but there is an attempt to remove as many of these 'combinatorial' (in this case, 'reverse' x 'start, done') function names in Base as possible in favor of relying on dispatch.

@ararslan
Copy link
Member

ararslan commented Oct 3, 2017

If we do this, that would likely simplify some stuff in my RevString excision, though we're essentially reintroducing a RevString-like type.

@stevengj
Copy link
Member Author

stevengj commented Oct 4, 2017

@ararslan, not quite, because Reverse will be a subtype of Any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets design Design of APIs or of the language itself
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants