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

last(itr) should use Iterators.reverse #43497

Closed
wants to merge 3 commits into from
Closed

last(itr) should use Iterators.reverse #43497

wants to merge 3 commits into from

Conversation

stevengj
Copy link
Member

As I commented in #42225, we should really default to first(Iterators.reverse(itr)) for last(itr), rather than defaulting to itr[end], similar to how #34868 implemented last(itr, n).

This way you automatically get last for any iterable object that supports reverse iteration — otherwise, last(itr, n) would work but last(itr) will fail, which is weird.

I put in an last(a) = a[end] fast path for AbstractArray and Tuple.

In principle, this could be a breaking, if an object is indexable but doesn't support reverse iteration, but this seems like it should be uncommon?

PS. Currently, Cmd supports iteration and indexing but not reverse iteration, so I added an explicit fix for that type here. I'll make a separate PR that adds reverse iteration for Cmd, which is a cleaner fix.

@stevengj stevengj added speculative Whether the change will be implemented is speculative iteration Involves iteration or the iteration protocol labels Dec 20, 2021
@KristofferC
Copy link
Member

In principle, this could be a breaking, if an object is indexable but doesn't support reverse iteration, but this seems like it should be uncommon?

Heh, this PR already happened and then got reverted #43106.

@stevengj
Copy link
Member Author

I see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iteration Involves iteration or the iteration protocol speculative Whether the change will be implemented is speculative
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants