Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function Base.permutedims(A::KeyedArray, perm)
KeyedArray(data, new_keys)#, copy(A.meta))
end

if VERSION >= v"1.1"
# This copies the implementation from Base, except with numerical_dims:
@inline function Base.eachslice(A::KeyedArray; dims)
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
length(numerical_dims) == 1 || throw(ArgumentError("only single dimensions are supported"))
dim = first(numerical_dims)
dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions"))
inds_before = ntuple(d->(:), dim-1)
inds_after = ntuple(d->(:), ndims(A)-dim)
return (view(A, inds_before..., i, inds_after...) for i in axes(A, dim))
end
end

function Base.mapslices(f, A::KeyedArray; dims)
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
data = mapslices(f, parent(A); dims=dims)
Expand Down
5 changes: 5 additions & 0 deletions test/_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ VN = NamedDimsArray(V.data.data, v=10:10:100)
axiskeys(V2', 2)[1] = :zed
@test axiskeys(V2,1) == [:zed, :b, :c]

# eachslice
if VERSION >= v"1.1"
@test axiskeys(first(eachslice(M, dims=:r))) === (2:5,)
end

# mapslices
@test axiskeys(mapslices(identity, M, dims=1)) === (Base.OneTo(3), 2:5)
@test axiskeys(mapslices(sum, M, dims=1)) === (Base.OneTo(1), 2:5)
Expand Down