Skip to content

Commit 397f7e6

Browse files
committed
add stack
1 parent fa04978 commit 397f7e6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/functions.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ elseif VERSION >= v"1.1"
113113
end
114114
end
115115

116+
@static if VERSION > v"1.9-DEV"
117+
function Base.stack(A::KeyedArray; dims::Colon=:)
118+
data = @invoke Base.stack(A::AbstractArray; dims)
119+
if !allequal(named_axiskeys(a) for a in A)
120+
throw(DimensionMismatch("stack expects uniform axiskeys for all arrays"))
121+
end
122+
akeys = (; named_axiskeys(first(A))..., named_axiskeys(A)...)
123+
KeyedArray(data; akeys...)
124+
end
125+
end
126+
116127
function Base.mapslices(f, A::KeyedArray; dims)
117128
numerical_dims = NamedDims.dim(A, dims)
118129
data = mapslices(f, parent(A); dims=numerical_dims)

test/_functions.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ A3 = wrapdims(rand(Int8, 3,4,2), r='a':'c', c=2:5, p=[10.0, 20.0])
4646
if VERSION >= v"1.1"
4747
@test axiskeys(first(eachslice(M, dims=:r))) === (2:5,)
4848
end
49+
if VERSION >= v"1.9-DEV"
50+
arr = KeyedArray([
51+
KeyedArray([1, 2], a=[:x, :y]),
52+
KeyedArray([3, 4], a=[:x, :y]),
53+
KeyedArray([5, 6], a=[:x, :y]),
54+
], b=10:12)
55+
56+
sk = Base.stack(arr)::KeyedArray
57+
@test sk == [1 3 5; 2 4 6]
58+
@test named_axiskeys(sk) == (a=[:x, :y], b=10:12)
59+
end
4960

5061
@test axiskeys(selectdim(M, :r, [true, false, true])) == (['a', 'c'], 2:5)
5162

0 commit comments

Comments
 (0)