-
-
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
Add keys(::Array) and values(::Array) #12811
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -981,3 +981,18 @@ end | |
symdiff(a) = a | ||
symdiff(a, b) = union(setdiff(a,b), setdiff(b,a)) | ||
symdiff(a, b, rest...) = symdiff(a, symdiff(b, rest...)) | ||
|
||
# Lightweight wrapper type for values(::Array) | ||
immutable ArrayIterator{T,N} | ||
array::Array{T,N} | ||
end | ||
start(ai::ArrayIterator) = start(ai.array) | ||
next(ai::ArrayIterator,k) = next(ai.array,k) | ||
done(ai::ArrayIterator,k) = done(ai.array,k) | ||
|
||
length(v::ArrayIterator) = length(v.array) | ||
isempty(v::ArrayIterator) = isempty(v.array) | ||
eltype{T,N}(::Type{ArrayIterator{T,N}}) = T | ||
|
||
keys(x::Array) = CartesianRange(size(x)) | ||
values(x::Array) = ArrayIterator(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this equivalent to just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is not to allow mutation of the underlying array with something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have this at all, I would prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its main purpose is for iteration, but that's certainly not the only way it could be used. Maybe this approach is too paternalistic, though. |
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.
Why not
keys(x::AbstractArray) = eachindex(x)
?And since arrays are already iterable, why not just(discussed below)values(x::AbstractArray) = x
?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'm fine with that as well.
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.
Actually wait,
eachindex(x)
just gives the linear indexing1:n
. I prefer the multidimensional Cartesian range since you can use the individual components directly: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.
eachindex
chooses the most efficient index iterator for the given array. ForArray
and other contiguous arrays (LinearFast()
), that's simply1:n
. But forSparseMatrixCSC
and other non-contiguous arrays (LinearSlow()
), it's a cartesian range.