<div dir="ltr"><div>Hello Martin, Alex</div><div><br></div>This is interesting as this works<div><br></div><div><div>get_attr(Attr, Map) -></div><div> #{Attr := Val} = Map, Val.</div></div><div><br></div><div>It is kind of documented that only matching literal keys in function heads are supported:</div><div><a href="http://www.erlang.org/doc/reference_manual/expressions.html#id81375">http://www.erlang.org/doc/reference_manual/expressions.html#id81375</a> under Matching Syntax<font color="#000000" face="Verdana, Arial, Helvetica, sans-serif" size="3">.</font></div><div><br></div><div>I wonder how complicated it would be to support the syntax in question.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 29, 2015 at 5:57 PM, Martin Koroudjiev <span dir="ltr"><<a href="mailto:mrtndimitrov@gmail.com" target="_blank">mrtndimitrov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello and thank,<br>
<br>
Yes, I know about the maps module but was trying to explore the map<br>
syntax and use pattern matching. I, actually, am not trying to match on<br>
the value but on the key name. I thought Attrib will take the value of<br>
the first argument and this value will be used as key name in the map.<br>
<br>
Regards,<br>
Martin<br>
<div class="HOEnZb"><div class="h5"><br>
On 10/29/2015 6:21 PM, Alex Shneyderman wrote:<br>
> Attr might have multiple values.<br>
><br>
> io:format("b: ~p~n", maps:get(b, M)),<br>
> io:format("d: ~p~n", maps:get(d, M)).<br>
><br>
> is probably what you want.<br>
><br>
> It is discussed briefly in LYSE book:<br>
><br>
> "You also cannot do matching of a key by value (#{X := val} = Map)<br>
> because there could be multiple keys with the same value."<br>
><br>
> Cheers,<br>
> Alex.<br>
><br>
> On Thu, Oct 29, 2015 at 11:33 AM, Martin Koroudjiev<br>
> <<a href="mailto:mrtndimitrov@gmail.com">mrtndimitrov@gmail.com</a>> wrote:<br>
>> Hello,<br>
>><br>
>> How can I do pattern matching with maps in function heads? I tried<br>
>><br>
>> -module(m1).<br>
>><br>
>> -export([main/0]).<br>
>><br>
>> main() -><br>
>> M = #{a => 1, b => 2, c => 3},<br>
>> io:format("b: ~p~n", get_attr(b, M)),<br>
>> io:format("d: ~p~n", get_attr(d, M)).<br>
>><br>
>> get_attr(Attr, #{Attr := Val} = Map) -><br>
>> Val;<br>
>> get_attr(_Attr, _M) -><br>
>> not_found.<br>
>><br>
>> but I get compile error: m1.erl:10: variable 'Attr' is unbound<br>
>><br>
>> Thanks in advance,<br>
>> Martin<br>
>><br>
>> _______________________________________________<br>
>> erlang-questions mailing list<br>
>> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>