3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

口で言うより行うことがErlang習得への近道と信じています。

lists:keyreplace/4

keyreplace(term, integer, [tuple], tuple) -> [tuple]
keyreplace(検索ワード, タプルの要素の位置, タプルのリスト, 置換するタプル) -> 新しいタプルのリスト

lists:keyreplace/4は指定された位置の要素が検索ワードと等しいタプルを探し、該当するタプルが見つかった場合はそのタプルを指定されたタプルに置き換えます。置き換えられるのは最初に出現した一個だけです。

% 2番目の要素が「20」のタプルを「{"I", 123}」に置き換える
> lists:keyreplace(20, 2, [{"A", 10}, {"B", 20}, {"C", 30}], {"I", 123}).
[{"A",10},{"I",123},{"C",30}]

% 該当するタプルが見つからない場合は、元のリストのまま
> lists:keyreplace(foo, 2, [{"A", 10}, {"B", 20}, {"C", 30}], {"I", 123}).
[{"A",10},{"B",20},{"C",30}]

lists:keystore/4

lists:keystore/4lists:keyreplace/4と似てますが、検索ワードと等しいタプルが見つからない場合には指定されたタプルがリストの最後尾に追加されます。

% 2番目の要素が「20」のタプルを「{"I", 123}」に置き換える
> lists:keystore(20, 2, [{"A", 10}, {"B", 20}, {"C", 30}], {"I", 123}).
[{"A",10},{"I",123},{"C",30}]

% 該当するタプルが見つからない場合は、最後尾に追加
> lists:keystore(foo, 2, [{"A", 10}, {"B", 20}, {"C", 30}], {"I", 123}).
[{"A",10},{"B",20},{"C",30},{"I",123}]

Elixirにも挑戦したい

闘魂ElixirシリーズとElixir Schoolがオススメです。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?