pattern matching & map

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

pattern matching & map

Oleg-2
  Hi, all.

I try to do the pattern matching in a function definition with maps:

add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->

But erlc says me:

variable 'Name' is unbound

Is this is not supported yet?

Erlang/OTP 20

--
Олег Неманов (Oleg Nemanov)
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

zxq9-2
On 2018年11月26日月曜日 12時21分25秒 JST Oleg wrote:

>   Hi, all.
>
> I try to do the pattern matching in a function definition with maps:
>
> add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
>
> But erlc says me:
>
> variable 'Name' is unbound
>
> Is this is not supported yet?

You can match on literals as maps keys in a function head, but not dynamic values.

I believe you can match on dynamic values within a function body in a case:


  add_prms([{Name, Val} | T], Prms) ->
      case Prms of
          #{Name := Vals} -> % ...
  % ...


-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

Michał Muskała
In reply to this post by Oleg-2
In OTP 21 you could use the map_get/2 guard:

add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->

Michał.
On 26 Nov 2018, 10:57 +0100, Oleg <[hidden email]>, wrote:
Hi, all.

I try to do the pattern matching in a function definition with maps:

add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->

But erlc says me:

variable 'Name' is unbound

Is this is not supported yet?

Erlang/OTP 20

--
Олег Неманов (Oleg Nemanov)
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

Oleg-2
In reply to this post by zxq9-2
On Mon, Nov 26, 2018 at 06:58:48PM +0900, [hidden email] wrote:

> On 2018年11月26日月曜日 12時21分25秒 JST Oleg wrote:
> >   Hi, all.
> >
> > I try to do the pattern matching in a function definition with maps:
> >
> > add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
> >
> > But erlc says me:
> >
> > variable 'Name' is unbound
> >
> > Is this is not supported yet?
>
> You can match on literals as maps keys in a function head, but not dynamic values.
>
> I believe you can match on dynamic values within a function body in a case:
>
>
>   add_prms([{Name, Val} | T], Prms) ->
>       case Prms of
>           #{Name := Vals} -> % ...
>   % ...

Already done like this :-), but thought that i do something wrong with map pattern matching in function.

Thanks!

--
Олег Неманов (Oleg Nemanov)
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

Oleg-2
In reply to this post by Michał Muskała
On Mon, Nov 26, 2018 at 11:12:39AM +0100, Michał Muskała wrote:
> In OTP 21 you could use the map_get/2 guard:
>
> add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->

Wow. That's great! But why we can't simply do:

add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->

:-)?

--
Олег Неманов (Oleg Nemanov)
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

zxq9-2
On 2018年11月26日月曜日 14時12分24秒 JST Oleg wrote:

> On Mon, Nov 26, 2018 at 11:12:39AM +0100, Michał Muskała wrote:
> > In OTP 21 you could use the map_get/2 guard:
> >
> > add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->
>
> Wow. That's great! But why we can't simply do:
>
> add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
>
> :-)?

IIRC is is because `Name` isn't really being used to match (unify) there, it is being used to call into map code, which is slightly different and only abstracted by the magical #{} syntax. There is an explanation of this somewhere on list, but I don't have the link handy.

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

Per Hedeland
On 2018-11-26 12:16, [hidden email] wrote:

> On 2018t1126åÜå 14B1224Ò JST Oleg wrote:
>> On Mon, Nov 26, 2018 at 11:12:39AM +0100, MichaB MuskaBa wrote:
>>> In OTP 21 you could use the map_get/2 guard:
>>>
>>> add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->
>>
>> Wow. That's great! But why we can't simply do:
>>
>> add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
>>
>> :-)?
>
> IIRC is is because `Name` isn't really being used to match (unify) there, it is being used to call into map code, which is slightly different and only abstracted by the magical #{} syntax. There is an explanation of this somewhere on list, but I don't have the link handy.

AFAIR the "problem" is that the order of evaluation of function
arguments is undefined. The #{Name := Vals} matching requires that
'Name' is bound (there is no "magic" AFAIK) - and it *would* be in
the above function head, if argument evaluation was defined to be
left-to-right.

--Per Hedeland
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

Per Hedeland
Btw,
http://erlang.org/doc/reference_manual/expressions.html#maps-in-guards
lists is_map/1 and map_size/1, which are also documented as "Allowed in
guard tests" in the erlang(3) man page (a.k.a.
http://erlang.org/doc/man/erlang.html) - while the new-in-21 BIFs
map_get/2 and is_map_key/2 are neither listed nor so documented (only
the README mentions that they can be used in guards AFAICS).

--Per

On 2018-11-26 12:35, Per Hedeland wrote:

> On 2018-11-26 12:16, [hidden email] wrote:
>> On 2018t1126åÜå 14B1224Ò JST Oleg wrote:
>>> On Mon, Nov 26, 2018 at 11:12:39AM +0100, MichaB MuskaBa wrote:
>>>> In OTP 21 you could use the map_get/2 guard:
>>>>
>>>> add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->
>>>
>>> Wow. That's great! But why we can't simply do:
>>>
>>> add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
>>>
>>> :-)?
>>
>> IIRC is is because `Name` isn't really being used to match (unify) there, it is being used to call into map code, which is slightly different and only abstracted by the magical #{} syntax. There is an explanation of this somewhere on list, but I don't have the link handy.
>
> AFAIR the "problem" is that the order of evaluation of function
> arguments is undefined. The #{Name := Vals} matching requires that
> 'Name' is bound (there is no "magic" AFAIK) - and it *would* be in
> the above function head, if argument evaluation was defined to be
> left-to-right.
>
> --Per Hedeland
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
by
Reply | Threaded
Open this post in threaded view
|

Re: pattern matching & map

by
In reply to this post by Oleg-2
I believe below link would be a good reference.
And yes, this feature is not yet implemented even in Erlang/OTP 21.
I encounter the same problem when I read the second edition of joe’s Erlang book.

Best Regards,
Randall

在 2018年11月26日,17:21,Oleg <[hidden email]> 写道:

 Hi, all.

I try to do the pattern matching in a function definition with maps:

add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->

But erlc says me:

variable 'Name' is unbound

Is this is not supported yet?

Erlang/OTP 20

--
Олег Неманов (Oleg Nemanov)
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions