Erlang の maps モジュールを一通り試す - その4
OTP 17.0 から maps モジュールが追加された。一通り試す。
公式URL:http://erlang.org/doc/man/maps.html
update/3 関数
update(Key, Value, Map1) -> Map2
Types
Map1 = #{Key := term(), term() => term()} Map2 = #{Key := Value, term() => term()}
Map1
にKey
が存在する場合、その値はValue
に置き換えられ、新しいマップMap2
として返す。
Map1
がマップでない場合、呼び出しは{badmap, Map}
例外で失敗する。Key
に関連付けられてた値がない場合は{badkey, Key}
例外で失敗する。
Example:
> M = #{a=>1, b=>2}. #{a => 1,b => 2} > maps:update(a, 3, M). #{a => 3,b => 2} > maps:update(c, 3, M). ** exception error: {badkey,c} in function maps:update/3 called as maps:update(c,3,#{a => 1,b => 2})
update_with/3 関数
update_with(Key, Fun, Map1) -> Map2
Types
Map1 = #{Key := Value1, term() => term()} Map2 = #{Key := Value2, term() => term()} Fun = fun((Value1) -> Value2)
古い値でFun/1
を呼び出して新しい値を得えて、その値でKeyに関連付けられたMap1
の値を更新する。
キーがマップに存在しない場合、{badkey, Key}
例外で失敗する。
Example:
> Fun = fun(V1) -> V1 * 2 end. #Fun<erl_eval.7.126501267> > M = #{a=>1, b=>2}. #{a => 1,b => 2} > maps:update_with(a, Fun, M). #{a => 2,b => 2} > maps:update_with(c, Fun, M). ** exception error: {badkey,c} in function maps:update_with/3 called as maps:update_with(c,#Fun<erl_eval.7.126501267>,#{a => 1,b => 2})
update_with/4 関数
update_with(Key, Fun, Init, Map1) -> Map2
Types
Map1 = #{Key => Value1, term() => term()} Map2 = #{Key := Value2 | Init, term() => term()} Fun = fun((Value1) -> Value2)
古い値でFun/1
を呼び出して新しい値を得えて、その値でKeyに関連付けられたMap1
の値を更新する。 Map1
にキーが存在しない場合、Init
がキーに関連付けられる。
Example:
1> Fun = fun(V1) -> V1 * 2 end. #Fun<erl_eval.7.126501267> 2> M = #{a=>1, b=>2}. #{a => 1,b => 2} 3> maps:update_with(a, Fun, 10, M). #{a => 2,b => 2} 5> maps:update_with(c, Fun, 10, M). #{a => 1,b => 2,c => 10}
values/1 関数
values(Map) -> Values
Types
Map = #{term() => Value} Values = [Value]
マップMap
に含まれる値の完全なリストを、任意の順序で返す。
Map
がマップでない場合、呼び出しは{badmap, Map}
例外で失敗する。
Example:
> M = #{a=>1, b=>2}. #{a => 1,b => 2} > maps:values(M). [1,2]
with/2 関数
with(Ks, Map1) -> Map2
Types
Ks = [K] Map1 = #{K => V, term() => term()} Map2 = #{K => V}
マップMap1
からキーのリストKs
(K1
からKn
)に関連する値を持つ新しいマップMap2
を返す。 Map1
に存在しないKs
のキーは無視される。
Example:
> M = #{a=>1, b=>2, c=>3, d=>4}. #{a => 1,b => 2,c => 3,d => 4} > maps:with([a,b], M). #{a => 1,b => 2} > maps:with([d,f,g], M). #{d => 4} > maps:with([f,g], M). #{}
without/2 関数
without(Ks, Map1) -> Map2
Types
Ks = [K] Map1 = Map2 = map() K = term()
マップMap1
からキーのリストKs
(K1
からKn
)に関連しない値を持つ新しいマップMap2
を返す。 Map1
に存在しないKs
のキーは無視される。
Example:
> M = #{a=>1, b=>2, c=>3, d=>4}. #{a => 1,b => 2,c => 3,d => 4} > maps:without([a,b], M). #{c => 3,d => 4} > maps:without([d,f,g], M). #{a => 1,b => 2,c => 3} > maps:without([f,g], M). #{a => 1,b => 2,c => 3,d => 4} > maps:without([a,b,c,d], M). #{}