Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active December 16, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voluntas/5507279 to your computer and use it in GitHub Desktop.
Save voluntas/5507279 to your computer and use it in GitHub Desktop.
lists:filtermap/2 コトハジメ

lists:filtermap/2 コトハジメ

更新

2013-05-03

バージョン

0.0.1

作者

@voluntas

URL

http://voluntas.github.io/

概要

hash

https://github.com/erlang/otp/commit/3ac04f901ecfb3fed128759964ccc54fa4ee7b2a

R16B01 から非ドキュメントだった lists:zf/2 が lists:filtermap2/ として入るみたいです。

たまーに使いたくなる関数なのですが、非ドキュメントだったりして使うのを躊躇していたのですが今後は積極的に使えそうです。

Note

もちろん R16B01 でも zf/2 は残りますが filtermap/2 として使うことをお勧めします。

lists:zf/2

戻り値として true または false または {true, any()} を返すことでリストに入る値を変更できます。

  • true の場合はそのままもらった値
  • false の場合はスキップ
  • {true, any()} の場合は any() の値

サンプル

3> F = fun(N) ->
3>         case N of
3>             1 ->
3>                 true;
3>             3 ->
3>                 false;
3>             _ ->
3>                 {true, 10}
3>         end
3>     end,
3> lists:zf(F, [1,2,3,4,5]).
[1,10,10,10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment