denna

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

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

josevalim

Creator of Elixir

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

wolf4earth

Something which might be interesting for you could be :ets.fun2ms which transforms a function to an ets match specification.

hauleth

hauleth

Well, partially you can by using match spec and then using :erlang.match_spec_test([value], match_spec, :table).

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement