-
-
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
keys, eachindex and linearindices consistency #25901
Comments
💯 for "indices" terminology everywhere. IMO "keys" for arrays is pretty nonsensical to a layperson without explanation. While I honestly I don't see the need to unify terminology in array and dictionary interfaces, I think "index" and "indices" are much better and more descriptive names in general. (Certainly "index" for a dictionary is better than "key" for an array...) |
Couldn't we rename both |
I agree, but the stumbling block is that we want to use |
There's an ambiguity as to whether you're passing indices or an array. That should be resolved anyway, but will definitely need to be resolved to replace |
One solution would be to require passing a tuple of ranges for indices, i.e. |
Triage likes the idea of making |
Not directly a solution to the proposal, but a related idea that might be helpful here: How about not allowing linear indexing in arrays with a plain A general array would then only have the following methods getindex(::AbstractArray{T,N}, I::Vararg{Int,N}}) where {T,N}
getindex(::AbstractArray{T,N}, i::LinearIndex) where {T,N}
Another advantage would be that new types of indices would be allowed. You could directly index into the parent of a |
And almost too trivial to mention, the similarity/analogy with |
I would be very interested if you would care to elaborate on the downvote, @stevengj ? I don't quite see the downside of this. Most of the times you will get your linear index from an iterator such as |
Anything that requires wrapping another type around the indices makes things harder to work with, especially for newcomers. For casual exploration and prototyping, one usually doesn't work too hard at making code extremely generic to handle unusual cases (non 1-based arrays), and typing It's important to recognize that there is a downside to every added layer of abstraction we impose. I'm not convinced that we actually gain much by a linear-index abstraction. |
Ok, while that makes sense to some extent, I am wondering in how many cases casual exploration and prototyping leads to linear indexing into a higher-dimensional array. Anyway, not something I care strongly about, just thought it was a useful proposal, for consistency and extensibility. |
My thoughts on what makes linear indices special: as opposed to
Note that 2. and 3. can't both hold for offset vectors (higher dimensional offset arrays are fine). Regarding 1. I feel we are eventually going to need a more formalized token system for Regarding 3. I have wondered whether (a) an interface for getting/setting the Sorry, I really have more questions than answers here. I would be glad to be rid of the |
@timholy What are your thoughts about deprecating |
Actually julia> x = TestHelpers.OAs.OffsetArray(1:3, (3,))
Main.TestHelpers.OAs.OffsetArray{Int64,1,UnitRange{Int64}} with indices 4:6:
1
2
3
julia> linearindices(x)
4:6
julia> LinearIndices(axes(x))
LinearIndices{1,Tuple{UnitRange{Int64}}} with indices 4:6:
1
2
3
julia> y = TestHelpers.OAs.OffsetArray([1 2; 3 4], (3, 2))
Main.TestHelpers.OAs.OffsetArray{Int64,2,Array{Int64,2}} with indices 4:5×3:4:
1 2
3 4
julia> linearindices(y)
Base.OneTo(4)
julia> LinearIndices(axes(y))
LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}} with indices 4:5×3:4:
1 3
2 4 It's kind of confusing to have two functions whose names differ only in case and which behave so differently. The behavior of |
The naming of our multiple index-related functions appears to be inconsistent to me despite recent improvements. In particular, the most generic function returning the natural type of indices for collections is now
keys
, but the most frequent type of collection in Julia is certainly arrays, for which we use the term "index"/"indices" everywhere else. Then we haveeachindex
which returns the most efficient type of indices (linear or cartesian depending on array types), andlinearindices
which always returns linear indices.linearindices(x)
can be deprecated in favor ofLinearIndices(x)
, which already works, and which is parallel toCartesianIndices(x)
, which also works.eachindex
, likefastindices
.indices
is now deprecated, maybe we could use that term instead ofkeys
in 1.0 (not in 0.7). This was indeed proposed in the PR which renamedindices
toaxes
. As it's been noted elsewhere, it sounds more acceptable to say that a dict has indices than to say that an array has keys. It's also more consistent withgetindex
/setindex
. That way we could avoid saying "indices or keys" in the docs (e.g. forfindall
) and just say "indices" everywhere.haskey
would have to be changed tohasindex
(which could BTW be unified withcheckbounds
).Cc: @andyferris @timholy
The text was updated successfully, but these errors were encountered: