-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
make last
work on reversible any collection
#42991
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will need to delete
Line 106 in aca020b
first(r::Reverse) = last(r.itr) # and the last shall be first |
Ah of course. What would be an appropriate replacement for this? I suppose we could use first(r::Reverse) = foldl((_, x) -> x, r.itr) I think if |
I think that line can just be deleted, so it falls back to the generic |
Okay, let's see what happens |
Ah, I guess you might have to keep the old version for |
Any reason to not keep it for |
That's probably fine, although I don't think we generally assume all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Simeon Schaub <[email protected]>
@@ -103,7 +103,6 @@ size(r::Reverse) = size(r.itr) | |||
IteratorSize(::Type{Reverse{T}}) where {T} = IteratorSize(T) | |||
IteratorEltype(::Type{Reverse{T}}) where {T} = IteratorEltype(T) | |||
last(r::Reverse) = first(r.itr) # the first shall be last | |||
first(r::Reverse) = last(r.itr) # and the last shall be first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need to put this back, with a special attempt not to form an infinite recursion, but let's wait for PkgEval to determine that.
@MasonProtter This PR has broken DataFrames.jl tests. I assume the reason is it indirectly introduced triggering of the following:
I have not looked at the issue in detail but probably adding:
should be enough. |
Fixes the problem mentioned in JuliaLang#42991 (comment)
This could definitely break a lot of things, since |
@@ -476,7 +480,8 @@ julia> last([1; 2; 3; 4]) | |||
4 | |||
``` | |||
""" | |||
last(a) = a[end] | |||
last(a) = first(Iterators.reverse(a)) | |||
last(a::AbstractVector) = a[end] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be for AbstractArray
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that above, but @simeonschaub mentioned that since there are abstract arrays that don't support linear indexing, this might be the better default. Can give AbstractArray
a try though and see how it goes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, all arrays support linear indexing, but it just isn't required to be the most efficient index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array can also have a lastindex
that isn't necessarily an integer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so too, but lastindex
is actually defined to always return an integer.
In that case, would it make sense to have an |
Fixes the problem mentioned in #42991 (comment)
This reverts commit 1f484c3 since it seems to cause excess breakage for the benefits it brings.
Revert "make last work on any reversible collection (#42991), and fix it
Fixes the problem mentioned in JuliaLang#42991 (comment)
This reverts commit 1f484c3 since it seems to cause excess breakage for the benefits it brings.
Fixes the problem mentioned in JuliaLang#42991 (comment)
This reverts commit 1f484c3 since it seems to cause excess breakage for the benefits it brings.
As suggested by @simeonschaub in #42943 (comment), this changes the default fallback of
last(x)
fromx[end]
tofirst(Iterators.reverse(x))
, which allows it to work on any collection that supports being reversed, rather than just collections that have indexing.Before:
After:
Fixes #42943