A better pattern matching

77 views
Skip to first unread message

Siraj Kakeh

unread,
May 22, 2018, 7:38:26 PM5/22/18
to elixir-lang-core
Hello!
I have been working with Elixir on production level for 6 months. I find myself regularly doing pattern matching similar to this way: 
%{"id" => id,firstname: firstname, ...} = person_map
I come from a Javascript background and recently destructing was added to JS that pretty much looks like pattern matching in Elixir with the difference that I was able to do this in JS:
{ id, firstName } = personObject
 please don't mind the camel case, I know how sensitive Elixir developers are to that 😄
I was wondering if there was a way to make pattern matching like that in Elixir, for example the code above will be something like this:
%{ idfirstname, ...} = person_map
Maybe make that possible only on atom map keys to avoid converting between strings and atoms, or add a macro that does that.
I would love to hear your opinion, it's something that is done all the time on pattern matching and honestly I'm a little jealous JS can do that but Elixir `the mother of pattern matching` can't!

Andrea Leopardi

unread,
May 22, 2018, 7:42:05 PM5/22/18
to elixir-l...@googlegroups.com
Hey there,

there is no built-in way to do this. However, thanks to Elixir's extensibility, there are libraries that let you do something very similar to it. Check out https://github.com/whatyouhide/short_maps for example.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/9be39b53-4884-4ca2-adfb-d1dd7c4223f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Andrea Leopardi

Peter Hamilton

unread,
May 22, 2018, 7:42:58 PM5/22/18
to elixir-l...@googlegroups.com

--
Reply all
Reply to author
Forward
0 new messages