-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow syntactic sugar d[a, b, c] == d[(a, b, c)]
#87
Comments
d[a, b, c] == d[(a, b, c)]
d[a, b, c] == d[(a, b, c)]
Yes, it's definitely something we should look at here. What really needs to happen is to come up with a cohesive plan for multi-dimensional dictionaries. Sometimes the indices are Cartesian products of indices, and sometimes they could just be a subset. What interface do we want to support all this? What about the tokens for Cartesian products of indices - are they literally a Cartesian product of tokens and we have e.g. a matrix-like array backing the dictionary]? Do we use ragged arrays? One obvious corner case with this syntax (which happens in Base) is the one-element version doesn't create tuples, but it makes sense to have Cartesian products of zero or one sets. Sometimes I wonder if this should have similar syntax rules to tuples, where |
I would love to have this, I have an immediate use case and have started designing a simplistic multi-dimensional dictionary type as an AbstractDictionary subtype, with the multidimensional indexing syntax proposed in the first post. Indeed, I came across the same exact design questions you mention: how does slicing and subset indexing work? What are the underlying keys, and how do we map to them from a variety of inputs like subsets and slicing? What is the underlying data structure, if not a Vector? What happens in the case of a single dimension? I started going with the design that the underlying keys are just I didn't go with a general underlying array data structure since for my case in general I won't have rectangular data (more like ragged arrays). The issue is that I want to support slicing like Additionally, there is ambiguity with other syntax like If you want rectangular data, my conclusion seemed to be that the way to go would be to have an underlying array and then a Dictionary for each dimension mapping multidimensional keys to multidimensional cartesian indices which are linear in each dimension, like the design of julia> using NamedArrays
julia> n = NamedArray(rand(2, 4))
2×4 Named Matrix{Float64}
A ╲ B │ 1 2 3 4
──────┼───────────────────────────────────────────
1 │ 0.512214 0.700272 0.2982 0.211813
2 │ 0.165471 0.912168 0.0165095 0.873511
julia> setnames!(n, ["one", "two"], 1)
(OrderedCollections.OrderedDict{Any, Int64}("one" => 1, "two" => 2), OrderedCollections.OrderedDict{Any, Int64}("1" => 1, "2" => 2, "3" => 3, "4" => 4))
julia> setnames!(n, ["a", "b", "c", "d"], 2)
(OrderedCollections.OrderedDict{Any, Int64}("one" => 1, "two" => 2), OrderedCollections.OrderedDict{Any, Int64}("a" => 1, "b" => 2, "c" => 3, "d" => 4))
julia> n
2×4 Named Matrix{Float64}
A ╲ B │ a b c d
──────┼───────────────────────────────────────────
one │ 0.512214 0.700272 0.2982 0.211813
two │ 0.165471 0.912168 0.0165095 0.873511
julia> n.
array dicts dimnames
julia> n.array
2×4 Matrix{Float64}:
0.512214 0.700272 0.2982 0.211813
0.165471 0.912168 0.0165095 0.873511
julia> n.dicts
(OrderedCollections.OrderedDict("one" => 1, "two" => 2), OrderedCollections.OrderedDict("a" => 1, "b" => 2, "c" => 3, "d" => 4)) Not sure if there is a fundamental disadvantage to that design, but conceptually it seems very simple. So my conclusion is that you needed two separate types, |
Base defines the following for convenience when working with Dicts:
In the spirit of Dictionaries.jl, which allows us to deal with dictionaries in an array-like manner, would this be something nice to add?
It would be very convenient in some of my code, as it deals with variables that can be either Arrays or Dictionaries, meaning I have to worry about using either
d[a,b,c]
ord[(a,b,c)]
.The text was updated successfully, but these errors were encountered: