Skip to content

Commit 648ba1e

Browse files
authored
implement 1-arg cat/hcat/vcat (#115)
* implement 1-arg cat/hcat/vcat * fix tests * simplify hcat
1 parent 58bdfa4 commit 648ba1e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/functions.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ for (T, S) in [ (:KeyedArray, :KeyedArray),
173173
end
174174

175175
end
176+
# single argument
177+
Base.vcat(A::KeyedArray) = A
178+
function Base.hcat(A::KeyedArray)
179+
data = hcat(keyless(A))
180+
akeys = map(copy, (keys_or_axes(A, 1), keys_or_axes(A, 2)))
181+
KeyedArray(data, akeys)
182+
end
183+
function Base.cat(A::KeyedArray; dims)
184+
new_names = NamedDims.expand_dimnames(dimnames(A), dims)
185+
numerical_dims = NamedDims.dim(new_names, dims)
186+
data = cat(keyless(A); dims=dims)
187+
new_keys = ntuple(d -> keys_or_axes(A, d), ndims(data))
188+
KeyedArray(data, map(copy, new_keys)) # , copy(A.meta))
189+
end
190+
176191
val_strip(dims::Val{d}) where {d} = d
177192
val_strip(dims) = dims
178193
key_vcat(a::AbstractVector, b::AbstractVector) = vcat(a,b)

test/_functions.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ end
209209
@test_throws ArgumentError axiskeys(reduce(hcat, twomats'))
210210
@test_throws ArgumentError axiskeys(reduce(vcat, twomats))
211211

212+
# single argument
213+
@test dimnames(vcat(V)) == dimnames(cat(V; dims=1)) == (:r,)
214+
@test axiskeys(vcat(V)) == axiskeys(cat(V; dims=1)) == (['a', 'b', 'c'],)
215+
@test dimnames(hcat(V)) == dimnames(cat(V; dims=2)) == (:r, :_)
216+
@test axiskeys(hcat(V)) == axiskeys(cat(V; dims=2)) == (['a', 'b', 'c'], Base.OneTo(1))
217+
218+
@test dimnames(vcat(M)) == dimnames(cat(M; dims=1)) == (:r, :c)
219+
@test axiskeys(vcat(M)) == axiskeys(cat(M; dims=1)) == ('a':1:'c', 2:5)
220+
221+
@test dimnames(hcat(M)) == dimnames(cat(M, dims=2)) == (:r, :c)
222+
@test axiskeys(hcat(M)) == axiskeys(cat(M, dims=2)) == ('a':1:'c', 2:5)
223+
224+
@test dimnames(cat(M; dims=3)) == (:r, :c, :_)
225+
@test axiskeys(cat(M; dims=3)) == ('a':1:'c', 2:5, Base.OneTo(1))
212226
end
213227
@testset "matmul" begin
214228

0 commit comments

Comments
 (0)