Erlang の maps モジュールを一通り試す - その1
OTP 17.0 から maps モジュールが追加された。一通り試す。
公式URL:http://erlang.org/doc/man/maps.html
filter/2 関数
filter(Pred, MapOrIter) -> Map
Types
Pred = fun((Key, Value) -> boolean()) MapOrIter = #{Key => Value} | iterator(Key, Value) Map = #{Key => Value}
MapOrIter のうち Pred の関数が true になる Key と Value で構成されたマップ Map を返す。
Example:
> M = #{a => 2, b => 3, c => 4}. #{a => 2,b => 3,c => 4} > Pred = fun(K, V) -> V rem 2 =:= 0 end. #Fun<erl_eval.13.126501267> > maps:filter(Pred, M). #{a => 2,c => 4}
MapOrIterがマップまたは有効なイテレーターでない場合、{badmap、Map}
例外で呼び出しは失敗する。
> List = [{a,2},{b,3},{c,4}]. [{a,2},{b,3},{c,4}] > maps:filter(Pred, List). ** exception error: {badmap,[{a,2},{b,3},{c,4}]} in function maps:filter/2 called as maps:filter(#Fun<erl_eval.13.126501267>,[{a,2},{b,3},{c,4}])
Predがアリティ2の関数でない場合、badargで失敗する。
> Pred2 = fun(V) -> V rem 2 =:= 0 end. #Fun<erl_eval.7.126501267> > maps:filter(Pred2, M). ** exception error: bad argument in function maps:filter/2 called as maps:filter(#Fun<erl_eval.7.126501267>,#{a => 2,b => 3,c => 4})
find/2 関数
find(Key, Map) -> {ok, Value} | error
Types
Map = #{Key => Value, term() => term()}
Key
に関連付けられた値Value
をタプル{ok, Value}
で返す。Map
のKey
に値が関連付けられたValue
が無い場合はerror
を返す。
Example:
> M = #{alice => 42}. #{alice => 42} > maps:find(alice, M). {ok,42} > maps:find(bob, M). error
Map
がマップではない場合、呼び出しは{badmap,Map}
例外で失敗する。
> L = [{alice, 42}]. [{alice,42}] > maps:find(alice, L). ** exception error: {badmap,[{alice,42}]} in function maps:find/2 called as maps:find(alice,[{alice,42}])
fold/3 関数
fold(Fun, Init, MapOrIter) -> Acc
Types
Fun = fun((Key, Value, AccIn) -> AccOut) Init = term() Acc = AccOut AccIn = Init | AccOut MapOrIter = #{Key => Value} | iterator(Key, Value)
MapOrIter
のすべてのKey
とValue
の関連付けに対してFun(Key, Value, AccIn)
を任意の順序で呼び出す。 関数 Fun/3
は、次の連続する呼び出しに渡される新しいアキュムレーターを返さなければならない。この関数は、アキュムレーターの最後の値を返す。 マップが空の場合、初期アキュムレータ値Init
が返される。
Example:
> Fun = fun(K,V,AccIn) when is_atom(K) -> AccIn + V end. #Fun<erl_eval.19.126501267> > M = #{a => 1, b => 2, c => 3}. #{a => 1,b => 2,c => 3} > maps:fold(Fun, 0, M). 6
MapOrIterがマップまたは有効なイテレーターでない場合、{badmap、Map}
例外で呼び出しは失敗する。
> L = [{a,1},{b,1},{c,1}]. [{a,1},{b,1},{c,1}] > maps:fold(Fun, 0, L). ** exception error: {badmap,[{a,1},{b,1},{c,1}]} in function maps:fold/3 called as maps:fold(#Fun<erl_eval.19.126501267>,0,[{a,1},{b,1},{c,1}])
Fun
がアリティ3の関数でない場合、badarg
で失敗する。
> Fun2 = fun(V, AccIn) -> AccIn + V end. #Fun<erl_eval.13.126501267> > maps:fold(Fun2, 0, M). ** exception error: bad argument in function maps:fold/3 called as maps:fold(#Fun<erl_eval.13.126501267>,0,#{a => 1,b => 2,c => 3})
from_list/1 関数
from_list(List) -> Map
Types
List = [{Key, Value}] Key = Value = term() Map = map()
キーと値のタプル要素のリストを取得して、マップを作成する。 関連付けは任意の順序で行うことができ、関連付け内のキーと値はいずれの term()
でも良い。 同じキーが複数回出現する場合、後者(右端)の値が使用され、以前の値は無視される。
Example:
> L = [{a,1},{b,2},{c,3},{a,4}]. [{a,1},{b,2},{c,3},{a,4}] > maps:from_list(L). #{a => 4,b => 2,c => 3}
get/2 関数
get(Key, Map) -> Value
Types
Key = term() Map = map() Value = term()
マップMap
にキーKey
が含まれる場合、キーに関連付けられた値Value
を返す。
Example:
> M = #{a=>1,b=>2,c=>3}. #{a => 1,b => 2,c => 3} > maps:get(a, M). 1 > maps:get(b, M). 2
Keyに値が関連付けられていない場合は{badkey, Key}
例外で失敗する。
> M = #{a=>1,b=>2,c=>3}. #{a => 1,b => 2,c => 3} > maps:get(f, M). ** exception error: {badkey,f} in function maps:get/2 called as maps:get(f,#{a => 1,b => 2,c => 3})
Mapがマップでない場合、呼び出しは{badmap, Map}
例外で失敗する。
> L = [{a,1},{b,2},{c,3}]. [{a,1},{b,2},{c,3}] > maps:get(a, L). ** exception error: {badmap,[{a,1},{b,2},{c,3}]} in function maps:get/2 called as maps:get(a,[{a,1},{b,2},{c,3}])
get/3 関数
get(Key, Map, Default) -> Value | Default
Types
Map = #{Key => Value, term() => term()}
マップMap
にキーKey
が含まれる場合、キーに関連付けられた値Value
を返す。キーに値が関連付けられていない場合、Default
が返される。
Example:
> M = #{a=>1,b=>2,c=>3}. #{a => 1,b => 2,c => 3} > maps:get(a, M, 4). 1 > maps:get(b, M, 4). 2 > maps:get(f, M, 4). 4
Mapがマップでない場合、呼び出しは{badmap, Map}
例外で失敗する。
> L = [{a,1},{b,2},{c,3}]. [{a,1},{b,2},{c,3}] > maps:get(f, L, 4). ** exception error: {badmap,[{a,1},{b,2},{c,3}]} in function maps:get/3 called as maps:get(f,[{a,1},{b,2},{c,3}],4)