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

Why not just add iterable.reversed(): Iterable<T> to the iterable protocol and skip on the new symbol? #5

Open
dead-claudia opened this issue Nov 23, 2020 · 2 comments

Comments

@dead-claudia
Copy link

This IMHO would be much easier to use, and it would have benefits that extend beyond iteration, too.

  • If it doesn't have a .reversed method, it isn't reversible. I consider that far more intuitive than this proposal.
  • Arrays could implement this to return not merely an iterator, but an array. And this of course is just generally more useful, and for cases like .slice().reverse(), would provide a significant performance boost as you can use SIMD instructions to copy it in chunks. (In my experience, in perf-sensitive places, you're probably already writing a for loop anyways if you want reverse iteration, and if you really want a reverse iterator, you can just do .values().reversed(), .entries().reversed(), and so on like you'd likely already do with maps and sets.)
  • Adding it to iterable iterators alongside .next() would fit with the simple non-symbol name better, and one could imagine it being initialized with shared state.
  • Many userland data structures like Immutable already optimize for reversed collections by not actually copying them.
@dead-claudia dead-claudia changed the title Why not just make an iter.reversed(): Iterable<T> protocol and skip on the new symbol? Why not just make an iterable.reversed(): Iterable<T> protocol and skip on the new symbol? Nov 23, 2020
@dead-claudia
Copy link
Author

And as this isn't going to be adding additional language semantics that use .reversed(), I don't see any risk of conflict here unless, say, someone somewhere is defining an incompatible version of Array.prototype.reversed or something.

@dead-claudia dead-claudia changed the title Why not just make an iterable.reversed(): Iterable<T> protocol and skip on the new symbol? Why not just add iterable.reversed(): Iterable<T> to the iterable protocol and skip on the new symbol? Nov 23, 2020
@conartist6
Copy link

conartist6 commented Nov 23, 2020

I think I misunderstood this when I first read it. I see now that we're in line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants