# Is there a structure that combines NammedArray and DotMap functionality?

**URL:** <https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541>\
**Category:** General Usage\
**Tags:** nammedarray, dotmap\
**Created:** [October 31, 2022, 5:26am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541 "2022-10-31T05:26:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 31, 2022, 5:26am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/1 "2022-10-31T05:26:34Z")

</div>

```julia
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`

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 31, 2022, 6:02am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/2 "2022-10-31T06:02:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 31, 2022, 6:18am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/3 "2022-10-31T06:18:17Z")

</div>

Dictionaries or keyed arrays give you this functionality with almost the same syntax.

Dictionaries:

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 31, 2022, 6:43am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/4 "2022-10-31T06:43:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 31, 2022, 7:17am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/5 "2022-10-31T07:17:06Z")

</div>

If you wanted to use `KeyedArrays` as above, it looks like this might work:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [November 1, 2022, 12:26am UTC](https://discourse.julialang.org/t/is-there-a-structure-that-combines-nammedarray-and-dotmap-functionality/89541/6 "2022-11-01T00:26:52Z")

</div>

Thank you. That’s a nice approach. If its dangerous I might just stick with a few extra characters `x("b")`🙂
