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 ── 19. lists:filter

Last updated at Posted at 2023-01-10

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

lists:filter

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

リストの各要素に対して同じ処理を適用してtrueと評価される要素のみを含む新しいリストを生成します。

% リストから偶数のみを取り出す
> lists:filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]).
[2,4]

% 要素を取り出す条件の関数は真理値返さないとダメ
> lists:filter(fun(_X) -> "hello" end, [1, 2, 3, 4]).
** exception error: bad filter "hello"
     in function  lists:'-filter/2-lc$^0/1-0-'/2 (lists.erl, line 1383)

filter関数に馴染みのない方は闘魂Elixirの図を眺めてみるとなんとなく感覚が掴めるかもしれません。

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

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?