x = NamedArray([1,2,3],["a","b","c"])
x = DotMap( Dict( zip(["a","b","c"],[1,2,3]) ) )
So you could write both exp.(x)
and x.b
x = NamedArray([1,2,3],["a","b","c"])
x = DotMap( Dict( zip(["a","b","c"],[1,2,3]) ) )
So you could write both exp.(x)
and x.b
It wouldn’t be hard to make one, but why? this would determine the optimal underlying storage mechanism, or whether it’s even called for
Dictionaries or keyed arrays give you this functionality with almost the same syntax.
Dictionaries:
julia> using Dictionaries
julia> x = dictionary(zip(["a","b","c"], [1,2,3]))
3-element Dictionary{String, Int64}
"a" │ 1
"b" │ 2
"c" │ 3
julia> exp.(x)
3-element Dictionary{String, Float64}
"a" │ 2.718281828459045
"b" │ 7.38905609893065
"c" │ 20.085536923187668
julia> x["b"]
2
It’s an ordered dictionary where values can be mapped or broadcasted with the same performance as regular arrays.
And keyed arrays:
julia> using AxisKeys
julia> x = KeyedArray([1,2,3], ["a","b","c"])
1-dimensional KeyedArray(...) with keys:
↓ 3-element Vector{String}
And data, 3-element Vector{Int64}:
("a") 1
("b") 2
("c") 3
julia> exp.(x)
1-dimensional KeyedArray(...) with keys:
↓ 3-element Vector{String}
And data, 3-element Vector{Float64}:
("a") 2.718281828459045
("b") 7.38905609893065
("c") 20.085536923187668
julia> x("b")
2
Not a big thing.
x.b
is just a bit more succinct than x["b"]
or x("b")
so if you could use it then I would.
If you wanted to use KeyedArrays
as above, it looks like this might work:
Base.getproperty(x::KeyedArray, k::Symbol) = x(string(k))
though you might want to check the source code to make sure it doesn’t break anything.
Thank you. That’s a nice approach. If its dangerous I might just stick with a few extra characters x("b")