Skip to content

Support vectors as indices to NamedTuple #32664

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ New library functions
* New `findall(pattern, string)` method where `pattern` is a string or regex ([#31834]).
* `istaskfailed` is now documented and exported, like its siblings `istaskdone` and `istaskstarted` ([#32300]).
* `RefArray` and `RefValue` objects now accept index `CartesianIndex()` in `getindex` and `setindex!` ([#32653])
* `NamedTuple`s may now be subsetted with `getindex` ([#27021], [#32662])

Standard library changes
------------------------
Expand Down
9 changes: 9 additions & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ iterate(t::NamedTuple, iter=1) = iter > nfields(t) ? nothing : (getfield(t, iter
firstindex(t::NamedTuple) = 1
lastindex(t::NamedTuple) = nfields(t)
getindex(t::NamedTuple, i::Int) = getfield(t, i)
getindex(t::NamedTuple, i::Integer) = t[to_index(i)]
getindex(t::NamedTuple, i::Symbol) = getfield(t, i)
function getindex(t::NamedTuple, v::AbstractVector{Bool})
length(t) == length(v) || throw(BoundsError(t, v))
return t[to_index(v)]
end
getindex(t::NamedTuple, v::AbstractVector) = (; map(v) do i
k = i isa Symbol ? i : keys(t)[i]
return k => t[i]
end...)
indexed_iterate(t::NamedTuple, i::Int, state=1) = (getfield(t, i), i+1)
isempty(::NamedTuple{()}) = true
isempty(::NamedTuple) = false
Expand Down
13 changes: 13 additions & 0 deletions test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
@test (a=3,)[:a] == 3
@test (x=4, y=5, z=6).y == 5
@test (x=4, y=5, z=6).z == 6
@test (x=4, y=5, z=6)[1:1] == (x=4,)
@test (x=4, y=5, z=6)[1:2] == (x=4, y=5)
@test (x=4, y=5, z=6)[[1,3]] == (x=4, z=6)
@test (x=4, y=5, z=6)[[:x,:y]] == (x=4, y=5)
@test (x=4, y=5, z=6)[[:x,2]] == (x=4, y=5)
@test (x=4, y=5, z=6)[[true, true, false]] == (x=4, y=5)
@test (x=4, y=5, z=6)[[true, true, true]] == (x=4, y=5, z=6)
@test (x=4, y=5, z=6)[[false, false, false]] == NamedTuple()
@test (x=4, y=5, z=6)[Int[]] == NamedTuple()
@test_throws BoundsError (x=4, y=5, z=6)[[true, true]]
@test_throws ArgumentError (x=4, y=5, z=6)[true]
@test (x=4, y=5, z=6)[0x01] == (x=4, y=5, z=6)[Int16(1)] == 4
@test (x=4, y=5, z=6)[0x01:0x01] == (x=4, y=5, z=6)[[Int16(1)]] == (x=4,)
@test_throws ErrorException (x=4, y=5, z=6).a
@test_throws BoundsError (a=2,)[0]
@test_throws BoundsError (a=2,)[2]
Expand Down