2
2

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 ── 20. lists:filtermap

Last updated at Posted at 2023-01-10

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

lists:filtermap

filtermap(fun((term) -> boolean | {true, term}), [term]) -> [term]
filtermap(要素を取り出す条件, リスト) -> 新しいリスト

lists:filterの処理とlists:mapの処理を一度に実行します。第一引数に渡す関数は{true, 新しい値}もしくはfalseを返します。

% リストから偶数のみを取り出して1000加算する関数
IsEvenThenAdd1000 = fun(X) -> 
  case X rem 2 of 
    0 -> {true, X + 1000}; 
    1 -> false 
  end 
end.
> lists:filtermap(IsEvenThenAdd1000, [1, 2, 3, 4]).
[1002,1004]

listsモジュールには他にもリスト処理のための関数がたくさんあります。

Elixirにも挑戦したい

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

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?