2014-09-25 29 views
0

一個Erlang匹配規範支持與記錄以下方式在匹配規範的MatchHead:如何在匹配規範的MatchHead中用地圖替換記錄?

#recordName{field1=1, field='$1', _='_'} 

這所有recordName記錄從具有field1 == 1一個表匹配,也確實的field的隱式綁定可以在以後使用MatchBody。

是否有與地圖類似的東西?

我試過(除了谷歌)的語法如下(ES):

% ERROR: * 1: only association operators '=>' are allowed in map construction 
#{key:=1, key:='$1', _:='_'} 

% ERROR: * 1: illegal use of variable '_' in map 
#{key=>1, key=>$1', _=>'_'} 

是否有可能做到這一點和語法的地方記錄我找不到?或者,對地圖替換記錄的想法是錯誤的嗎?

TIA

編輯: 也許還不支持。剛剛看到this的帖子。

回答

1
1> M = #{k1 => 1, k2 => 2, k3 => 3}. 
#{k1 => 1,k2 => 2,k3 => 3} 
2> #{k1:=1,k2:=V} = M. 
#{k1 => 1,k2 => 2,k3 => 3} 
3> V. 
2 
4> %% but you cannot do 
4> ets:fun2ms(fun(#{key1:=V, key2:=R}) when V == 1 -> R end). 
Error: ets:fun2ms requires fun with single variable or tuple parameter 
{error,transform_error} 
5> 
5> %% while it is possible to do 
5> ets:fun2ms(fun({V,R}) when V == 1 -> R end). 
[{{'$1','$2'},[{'==','$1',1}],['$2']}] 
6> %% or 
6> rd(foo,{k1,k2}). 
foo 
7> ets:fun2ms(fun(#foo{k1=V,k2=R}) when V == 1 -> R end). 
[{#foo{k1 = '$1',k2 = '$2'},[{'==','$1',1}],['$2']}] 
8> %% or even 
8> ets:fun2ms(fun(#foo{k1=1,k2=R}) -> R end).    
[{#foo{k1 = 1,k2 = '$1'},[],['$1']}] 
9>