Split list of tuples into two lists

filtermap(Fun, List1) -> List2
OTP R16B01

Types

Fun = fun((Elem) -> boolean() | {true, Value})
List1 = [Elem]
List2 = [Elem | Value]
Elem = Value = term()

Calls Fun(Elem) on successive elements Elem of List1. Fun/1 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}.

Show

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]

seq(From, To) -> Seq

seq(From, To, Incr) -> Seq

Types

From = To = Incr = integer()
Seq = [integer()]

Returns a sequence of integers that starts with From and contains the successive results of adding Incr to the previous element, until To is reached or passed (in the latter case, To is not an element of the sequence). Incr defaults to 1.

Failures:

  • If To < From - Incr and Incr > 0.

  • If To > From - Incr and Incr < 0.

  • If Incr =:= 0 and From =/= To.

The following equalities hold for all sequences:

length(lists:seq(From, To)) =:= To - From + 1 length(lists:seq(From, To, Incr)) =:= (To - From + Incr) div Incr

Examples:

> lists:seq(1, 10). [1,2,3,4,5,6,7,8,9,10] > lists:seq(1, 20, 3). [1,4,7,10,13,16,19] > lists:seq(1, 0, 1). [] > lists:seq(10, 6, 4). [] > lists:seq(1, 1, 0). [1]

zipwith3(Combine, List1, List2, List3) -> List4

Types

Combine = fun((X, Y, Z) -> T)
List1 = [X]
List2 = [Y]
List3 = [Z]
List4 = [T]
X = Y = Z = T = term()

Combines the elements of three lists of equal length into one list. For each triple X, Y, Z of list elements from the three lists, the element in the result list is Combine(X, Y, Z).

zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3) is equivalent to zip3(List1, List2, List3).

Examples:

> lists:zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6], [7,8,9]). [12,15,18] > lists:zipwith3(fun(X, Y, Z) -> [X,Y,Z] end, [a,b,c], [x,y,z], [1,2,3]). [[a,x,1],[b,y,2],[c,z,3]]