-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
alist-like construct #8674
Comments
This seems similar to namedtuples in Python (in practice, not in implementation). In Python, they create a class with properties, but some sort of anonymous type seems like a treacherous road to go down. Perhaps the pair-tuple syntax could create a type that can be indexed numerically like a tuple but also accessed by field (overloading getfield). Could be implemented as an immutable type containing a tuple of values and a parallel tuple of symbols that can be searched. Illustration: function my_iterative_algorithm(start, tolerance, other_arguments...)
## algorithm goes here
(:root => root, :converged_p => convergence, :number_of_iterations => n, :precision => prec)
end
solution = my_iterative_algorithm(start, tol)
@assert solution.converged_p == solution[2]
if solution.converged_p:
do_something(solution.root)
else
try_something_else()
end As the other thread says, though, this seems like a job for composite types. |
How about just using function getindex{T}(t::(Pair{Symbol,T}...), key::Symbol)
for pair in t
pair.first == key && return pair.second
end
end ? Simple, quick (for small tuples), easy. This seems like something you could easily add to whatever package you're working on if you'd like to interface with tuples this way, but I'm not sure of the real utility in Base. In [2]: t = (:a=>1,:b=>2)
Out [2]: (:a=>1,:b=>2)
In [3]: t[:a]
Out [3]: 1 |
@iamed2: I agree that mature code should use composite types, but for quick prototyping I find them inflexible. @quinnj: Neat, thanks. The rationale for including this in |
This could be considered as part of a larger body of features offered by https://github.com/davidavdav/NamedArrays (or whatever they end up being called): arrays and tuples with names. I think this is a pretty basic feature, but it does not necessarily need to live in Base (maybe in a future set of basic packages). |
Also tuples of Pairs are faster than dict for small n (< 20) at least on 0.3.1 Benchmark: |
Here's another approach that requires keys to be known at compile time, but can avoid runtime key lookup and gives type information even for alists with heterogeneous values. Unfortunately for optimal performance this approach requires use of the |
see also #4916 |
As @JeffBezanson mentions, we've discussed a related need for a very lightweight |
If the keys are known at compile time, wouldn't something like perfect hashing be a better alternative? |
If the keys for both construction and lookup are known at compile time, you can just store the values in an array or tuple and map keys to indices at compile time (which is what my gist does). Minimal perfect hashing only seems useful if you have compile-time knowledge of the keys for construction but not lookup. Both approaches have the potential disadvantage of needing code specialized on different sets of keys. |
How would this be different than, say I really like the way the Python |
@PythonNut, for dictionaries with only a few elements (say, less than 20), something like the ideas presented here should be more efficient than creating a hash table.
|
Now that @JeffBezanson mentioned #4916, the parallels with LISP are even more apparent. In CL it is a common idiom to return a plist, which can be used with My experience with language/compiler design is nonexistent, but I think that Julia may need something like that. Eg there could be an abstract type Both would be interchangeable in terms of interface, except for performance, depending on the size. |
@tpapp, Julia already has an |
This seems to have been addressed by the addition of |
With pull request #8521 my understanding is that the
=>
has become the syntax for pairs (or similar) and has been decoupled fromDict
per se, so I am wondering if there is going to be support for lightweight associative lists, eg of the kindI guess I do not not need to argue very much that this is useful since the construct is present in all LISP languages (eg), but for me the most compelling argument is when I want to return multiple values but don't want to remember/hardcode the order of values, nor define a new type, and want something lighter than
Dict
(AFAIK hash tables of a few elements are not optimal from a performance point of view in most languages, but maybe Julia is different).An skeleton of an example could be
An advantage of Julia over LISP is that tuples are a separate type, so
[]
could dispatch on that and there is no need for a separate accessor function.(Sorry for the noise if this is already planned, I searched the archives and already asked a related question on julia-users but I was not aware of this new feature then.)
The text was updated successfully, but these errors were encountered: