Skip to content

Commit

Permalink
Add Dict(::NamedVector) method, fixes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Jun 21, 2018
1 parent e43dbe2 commit 3feb4ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -219,7 +219,7 @@ names(n::NamedArray, dim::Integer) ## just for dimension `dim`
dimnames(n::NamedArray) ## the names of the dimensions

@show names(n);
names(n) = Array{T,1} where T[String["one", "two"], Symbol[:a, :b, :c]]
names(n) = [String["one", "two"], Symbol[:a, :b, :c]]

@show names(n, 1)
names(n, 1) = String["one", "two"]
Expand All @@ -246,7 +246,7 @@ sets all the names of dimension `dim`, or only the name at index `index`, or the
copy(a::NamedArray)
```

returns a copy of all the elements in a, and copiess of the names, and returns a NamedArray
returns a copy of all the elements in a, and copies of the names, and returns a NamedArray

### Convert

Expand Down Expand Up @@ -322,4 +322,4 @@ These functions, when operating along one dimension, keep the names in the other

The current goal is to reduce complexity of the implementation. Where possible, we want to use more of the `Base.AbstractArray` implementation.

A longer term goal is to improve type stability, this might have a repercussion to the semantics of some operations.
A longer term goal is to improve type stability, this might have a repercussion to the semantics of some operations.
3 changes: 3 additions & 0 deletions src/base.jl
Expand Up @@ -52,3 +52,6 @@ Base.ind2sub(n::NamedArray, index::Integer) = tuple(map(x -> names(n, x[1])[x[2]

## simplified text representation of namedarray
Base.writedlm(io, n::NamedVecOrMat) = writedlm(io, hcat(names(n,1), n.array))

## Turn a NamedVector into a dict, #61
Base.Dict(n::NamedVector) = Dict(name => n[name] for name in names(n, 1))
8 changes: 7 additions & 1 deletion test/base.jl
Expand Up @@ -39,8 +39,14 @@ end
writedlm(STDOUT, n)
writedlm(STDOUT, v)

## Issue #60
## Issue #60
for func in [similar, zeros, ones, n -> hcat(n, n), n -> vcat(n, n)]
fn = @inferred func(n)
@test keytype.(fn.dicts) == (String, String)
end

## issue 61
d = Dict(v)
for key in keys(d)
@test d[key] == v[key]
end

0 comments on commit 3feb4ad

Please sign in to comment.