Given a simple dictionary d = Dict(1 => "a", 2 => "b", 3 => "c")
Is there a simple way to apply the dictionary to a list / vector / tuple of keys at once?
I would like to “translate” a vector of numbers into letters like
p = [1 3 2 1 2 1 3]
my current solutions include:
- using the get function:
get.(Ref(d), p, 0)
- using a map function:
map(p) do x
d[x]
end
- using a comprehension
[d[x] for x in p]
- or writing an eval function (for short hand notation)
dict_eval(dict, key) = get.(Ref(dict), key, 0)
using
dict_eval(d,p)
Do you have a suggestions for a better (short and fast = elegant) solution?
Concerning the syntax: My intuition would say that I would broadcast a dictionary to a vector of keys with a syntax like
d.[p]
but this is not (yet) a valid syntax in Julia.
Maybe this relates to the github issue 30836?
I am looking for a short syntax as this would help a lot in applying multiple dictionaries sequentially at once like (just think of encrypting text with different dictionaries like Caesar cypher)
caesar = Dict("a" => "d", "b" => "e", "c" => "f")
caesar.[d.[p]]