Skip to content

Commit

Permalink
Allow NamedTuple in constructor---this is the way to go, see #55
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Feb 20, 2020
1 parent e7860be commit d4bbaec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ NamedArray(array::AbstractVector{T},
dimname=defaultdimname(1)) where {T,VT} =
NamedArray(array, (names,), (dimname,))

function NamedArray(array::AbstractArray{T,N}, names::NamedTuple) where {T, N}
length(names) == N || error("Dimension mismatch")
return NamedArray(array, values(names), keys(names))
end

## Type and dimensions
"""
`NamedArray(T::Type, dims::Int...)` creates an uninitialized array with default names
Expand Down
6 changes: 6 additions & 0 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ n4 = @inferred NamedArray(a, (OrderedDict("a"=>1,"b"=>2), OrderedDict("C"=>1,"D"
@test names(n3, 1) == names(n4, 1) == ["a","b"]
@test names(n3, 2) == names(n4, 2) == ["C","D","E"]

## named tuples
n4 = @inferred NamedArray(a, (一=["α", "β"], 二=["أ", "ب", "ج"]))
@test dimnames(n4) == [:一, :二]
@test names(n4, 1) == ["α", "β"]
@test names(n4, 2) == ["أ", "ب", "ج"]

n1 = @inferred NamedArray([1], (BitArray([true]),))
n2 = @inferred NamedArray([1], BitArray([true]))
n3 = @inferred NamedArray([1], [true])
Expand Down

0 comments on commit d4bbaec

Please sign in to comment.