denna
Something like a Map with patterns as keys
I am brainstorming how to create a data-structure that contains anonymous functions that are used if a value matches a pattern. The data-structure needs to change dynamically.
A map seems to fit:
%{{1,2,3} => :func_a,{2,3,4} => :func_b}[{1,2,3}]
But the key is not a pattern it would be great to be able to do so.
I was hoping that it could be specified like a case statement:
case {1, 2, 3} do
{4, 5, 6} ->
"This clause won't match"
{1, 2, x} ->
"This clause will match and bind x to 3 in this clause"
_ ->
"This clause would match any value"
end
like this:
%{{1,2,_} => :func_a,{2,3,_} => :func_b}[{1,2,3}]
but this is not possible.
any suggestions?
Most Liked
dimitarvp
Not sure I get what you want but storing patterns like {1, 2, x} is not possible in Elixir.
Can you demonstrate one complete case of what would a successful implementation achieve and do?
josevalim
Maps are hash tables (for 32+ elements) indexed by key. Therefore a lookup such as {1, 2, _}, where we don’t fully have to key to hash, would require a linear traversal of all keys. In other words, maps are really not a good fit for the lookups you want to perform, regardless of Elixir having syntax for it or not.
Luckily, a relatively straight-forward solution is to nest your keys:
map = %{{1,2} => %{3 => :func_a},{2,3} => %{4 => :func_b}}
This way, if you want to lookup 1, 2, 3, you do:
map[{1, 2}][3]
if you want any (or all) keys starting with {1, 2}, then you do:
map[{1, 2}]
wolf4earth
Something which might be interesting for you could be :ets.fun2ms which transforms a function to an ets match specification.
hauleth
Well, partially you can by using match spec and then using :erlang.match_spec_test([value], match_spec, :table).







