diff --git a/src/functions.jl b/src/functions.jl index 134f2d2..79f2232 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -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) diff --git a/test/_functions.jl b/test/_functions.jl index 7925ffe..0a80a34 100644 --- a/test/_functions.jl +++ b/test/_functions.jl @@ -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)