LoginSignup
1
0

More than 5 years have passed since last update.

Erlang mapsの関数 "簡易" 説明

Posted at

Erlang mapsの関数 "簡易" 説明

mapsの関数はどんなものがあるのか、1行の簡単な説明とサンプルコードを記載。
詳しい情報は公式リファレンス参照。

元ネタ(公式リファレンス) http://erlang.org/doc/man/maps.html

すぐ機能を探して確認できて、どう動くかざっくり把握できるように記述。
解説の順番は、主観に基づく重要度で並べました。

このページのTODO:

  • ほぼ書きっぱなしなので、間違いがないか見直す。
  • 余裕があればもうちょっとマシな肉付けをする。しかし、どうせ書くなら、別のライブラリを増やしたいところ。
  • サンプルコードも要改善

空のMapsを作成する - maps:new/0

M0 = maps:new(),

Mapsに値を追加する - maps:put/3

M1 = maps:put(a, 1, M0),
M2 = maps:put(b, 2, M1),
M3 = maps:put(c, 3, M2),
M4 = maps:put(d, "abc", M3),
M5 = maps:put(e, <<"アイウエオ"/utf8>>, M4),   

Mapsから値を取得する - maps:get/2, maps:get/3

1 = maps:get(a, M5),
not_found = maps:get(g, M5, not_found),

Mapsから値を削除する - maps:remove/2

M4 = maps:remove(e, M5),
M3 = maps:remove(d, M4),

Mapsから値を探して取得する - maps:find/2

{ok,_} = maps:find(e, M5),
error  = maps:find(g, M5),

Mapsに指定のキーがあるか確認する - maps:is_key/2

true  = maps:is_key(a, M5),
false = maps:is_key(x, M5),

Mapsに登録されたデータ数を得る - size/1

5 = maps:size(M5),
1 = maps:size(M1),
0 = maps:size(M0),

Mapsに登録されたキーのリストを得る - keys/1

[a,b,c] = lists:sort( maps:keys(M3) ),

Mapsに登録された値のリストを得る - values/1

[1,2,3] = lists:sort( maps:values(M3) ),

Mapsをキーと値のタプルのリストにする - to_list/1

[{a,1},{b,2}] = maps:to_list(M2),

キーと値のタプルのリストからMapsを得る - from_list/1

M2 = maps:from_list([{a,1}, {b,2}]),

Mapsに任意のふるいをかけて新しいMapsを作る - filter/2

FilterdM = maps:from_list([{a,1},{c,3}]),
FilterdM = maps:filter(fun(_K, V) -> (V rem 2) =:= 1 end, M3),

Mapsに畳み込み演算を行う - fold/3

6 = maps:fold(fun(_, V, Acc) when is_integer(V) -> V+Acc;
                 (_, _, Acc) -> Acc
              end,
              0,
              M5),

Mapsの各値に対して任意の加工を行った結果を返す - map/2

NewMap = maps:from_list([{a,10},{b,20},{c,30}]),
NewMap = maps:map(fun(_K, V) -> V*10 end, M3),

2つのMapsをマージする - merge/2

% 同じキーがある場合、2つ目のMapsの値を優先する

MapA  = maps:from_list([{a,10},{b,20}]),
MapB  = maps:from_list([{a,1}, {c,30}]),
MapAB = maps:from_list([{a,1}, {b,20}, {c,30}]),
MapAB = maps:merge(MapA, MapB),

Mapsの任意のキーの値を更新する - update/3

UpdateMap1 = maps:from_list([{a,1},{b,2}]),
UpdateMap2 = maps:from_list([{a,10},{b,2}]),
UpdateMap2 = maps:update(a,10, UpdateMap1),

Mapsの任意のキーの値を関数を用いて更新する - update_with/3

UpdateMap2 = maps:update_with(a, fun(V) -> V*10 end, UpdateMap1),

update_with/3から、もしキーがなかった場合のデフォルト値を指定できる関数 - update_with/4

UpdateMap3 = maps:from_list([{a,1},{b,2},{c,3}]),
UpdateMap3 = maps:update_with(c, fun(V) -> V*10 end, 3, UpdateMap1),

Mapsから指定のキーのみでMapsを再作成する - with/2

WithKeys = [a,c],
WithMap1 = maps:from_list([{a,1},{b,2},{c,3}]),
WithMap2 = maps:from_list([{a,1},      {c,3}]),
WithMap2 = maps:with(WithKeys, WithMap1),

with/2と逆に指定のキー以外でMapsを再作成する - without/2

WithMap3 = maps:from_list([{b,2}]),
WithMap3 = maps:without(WithKeys, WithMap1),

Mapsから任意のキーの値と、そのキーを削除したMapsを返す - take/2

{2, M1} = maps:take(b, M2),
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0