-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Range first
and last
can be misleading
#22354
Comments
👍 to the |
I recall we looked at this once quite a while ago, and I think fixing it caused some performance problems. Maybe we'll fare better now though. |
In particular it might make sense to put it inside a |
An even more interesting/disturbing case: julia> last(1:-1:3)
2
julia> 1:-1:3 # Explanation
1:-1:2 |
There should probably be a special display for an empty range: julia> 1:1:-3
1:1:0
julia> 0:1:-3
0:1:-1
julia> -3:-1:1
-3:-1:-2 |
I agree this is strange, but it's used in APIs like
It'd definitely be worth seeing if we can replace it with a special function. |
I'd favor undocumenting that, though, and going the route suggested by @andyferris of adding |
I'm responsible for the return of a range by |
Funny how often this comes up this week. In the Search & Find Julep a solution to this was to wrap sorted vectors in a But AFAICT it would still be useful to know the insertion point, so a |
For triage: I think we'd better raise a |
I don't love the |
But not all ranges have a julia> fieldnames(StepRangeLen)
4-element Array{Symbol,1}:
:ref
:step
:len
:offset (Explanation: for |
Is this curious behavior only required for our implementation of |
I may be misunderstanding your question here, but it's used in packages too. For example, I'm almost sure it's exploited in AxisArrays. |
For what purposes? Regarding |
Looks like it's basically the same purpose as in Base: https://github.com/JuliaArrays/AxisArrays.jl/blob/master/src/search.jl |
OK. Though AFAICT the returned value is not used to insert entries, so we still don't have an example where the start of an empty index is useful. |
What's up with adding this to the milestone without triage, @nalimilan? And yes, I do keep a list of 1.0 issues open and refresh it several times a day to see if there are any more or less issues on it. |
Sorry, I was going to add a comment saying that the milestone should probably have been added when this issue was triaged. I then started to see whether it would be hard to make a PR and got distracted by other things. |
See PR #25385. It would be cool if triage could decide which approach is most appropriate. |
Nice. I had been thinking of making a simple PR which kind-of puns off |
Unfortunately I think we definitely need a completely new name for this — the question is which one: |
In the other thread I just proposed using properties to do |
I think we can punt on this for 1.0 --- it doesn't seem to be an urgent problem, largely a theoretical concern. In fact I think #25385 demonstrates that the existing behavior was largely ok and convenient. |
Hi, I tested this today on Julia 1.6.2 and got the same issue as the 'concerning' example above. Has there been a decision to not change this behaviour or is it worth keeping this open. julia> last(1:-1:3)
2
julia> 1:-1:3 # Explanation
1:-1:2
|
We should fix this. |
I've gotten bitten by empty ranges before in #40331. Wouldn't changing this be breaking tho? Also there should really be a special way to print an empty range, or make it clear that it is empty. |
We previously had defined it this way to avoid a branch when computing length (and instead do the branch on construction), but now we've put that branch back in most cases for length, so it would likely be worthwhile for someone to experiment with this change now, making it a possible "good first issue". |
What do you mean by "this change"? Clearly, changing the behavior of |
I think the surprising behavior is that the property julia> last(5:4)
4
julia> last(collect(5:4))
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [0] But I'm not sure what can be done about that, since the behavior of If julia> ([])[begin:end]
Any[] |
I noticed that
first(r)
andlast(r)
wherer
is one of various range types can give spurious results in the case the range is empty.I can see these are used to enable nice generic code for ranges, for various
Base
functions likeisempty
,issorted
and so-on. However, to me, this seems to violate the iterator interface pretty badly, and I would expect aBoundsError
thrown. Would it be more appropriate to use some specialized functions likerangestart
andrangestop
as an interface to get these numbers for ranges?The text was updated successfully, but these errors were encountered: