diff options
author | Anthony Ramine <[email protected]> | 2013-04-30 10:10:03 +0200 |
---|---|---|
committer | Anthony Ramine <[email protected]> | 2013-04-30 12:46:41 +0200 |
commit | 3ac04f901ecfb3fed128759964ccc54fa4ee7b2a (patch) | |
tree | 12e44041b8e9c15c8ab888081a4d7f336ecd7daa /lib/stdlib/doc/src/lists.xml | |
parent | 7e9ae3007fd3c44a05a746628983faff401dfd0c (diff) | |
download | otp-3ac04f901ecfb3fed128759964ccc54fa4ee7b2a.tar.gz otp-3ac04f901ecfb3fed128759964ccc54fa4ee7b2a.tar.bz2 otp-3ac04f901ecfb3fed128759964ccc54fa4ee7b2a.zip |
Rename and document lists:zf/2 as lists:filtermap/2
This function is used all over the place in OTP itself and people
sometimes want that functionality, they may as well not reimplement it
themselves.
Diffstat (limited to 'lib/stdlib/doc/src/lists.xml')
-rw-r--r-- | lib/stdlib/doc/src/lists.xml | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/stdlib/doc/src/lists.xml b/lib/stdlib/doc/src/lists.xml index b6c0fa4e05..1aff78f4fc 100644 --- a/lib/stdlib/doc/src/lists.xml +++ b/lib/stdlib/doc/src/lists.xml @@ -152,6 +152,31 @@ </desc> </func> <func> + <name name="filtermap" arity="2"/> + <fsummary>Filter and map elements which satisfy a function</fsummary> + <desc> + <p>Calls <c><anno>Fun</anno>(<anno>Elem</anno>)</c> on successive elements <c>Elem</c> + of <c><anno>List1</anno></c>. <c><anno>Fun</anno>/2</c> must return either a boolean + or a tuple <c>{true, <anno>Value</anno>}</c>. The function returns the list of elements + for which <c><anno>Fun</anno></c> returns a new value, where a value of <c>true</c> + is synonymous with <c>{true, <anno>Elem</anno>}</c>.</p> + <p>That is, <c>filtermap</c> behaves as if it had been defined as follows:</p> + <code type="none"> +filtermap(Fun, List1) -> + lists:foldr(fun(Elem, Acc) -> + case Fun(Elem) of + false -> Acc; + true -> [Elem|Acc]; + {true,Value} -> [Value|Acc] + end, + end, [], List1).</code> + <p>Example:</p> + <pre> +> <input>lists:filtermap(fun(X) -> case X rem 2 of 0 -> {true, X div 2}; _ -> false end end, [1,2,3,4,5]).</input> +[1,2]</pre> + </desc> + </func> + <func> <name name="flatlength" arity="1"/> <fsummary>Length of flattened deep list</fsummary> <desc> |