From 3ac04f901ecfb3fed128759964ccc54fa4ee7b2a Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 30 Apr 2013 10:10:03 +0200 Subject: 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. --- lib/stdlib/doc/src/lists.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/stdlib/doc/src/lists.xml') 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 @@ -151,6 +151,31 @@ true.

+ + + Filter and map elements which satisfy a function + +

Calls Fun(Elem) on successive elements Elem + of List1. Fun/2 must return either a boolean + or a tuple {true, Value}. The function returns the list of elements + for which Fun returns a new value, where a value of true + is synonymous with {true, Elem}.

+

That is, filtermap behaves as if it had been defined as follows:

+ +filtermap(Fun, List1) -> + lists:foldr(fun(Elem, Acc) -> + case Fun(Elem) of + false -> Acc; + true -> [Elem|Acc]; + {true,Value} -> [Value|Acc] + end, + end, [], List1). +

Example:

+
+> lists:filtermap(fun(X) -> case X rem 2 of 0 -> {true, X div 2}; _ -> false end end, [1,2,3,4,5]).
+[1,2]
+
+
Length of flattened deep list -- cgit v1.2.3