[erlang-questions] How to "chain" filters?

Steve Davis <>
Mon May 19 21:39:17 CEST 2008


To give a simple example below -- I wish to perform check2() only on the 
elements that pass check() and I can't see an elegant way to do it!

-module(filter).
-export([init/0]).

init() ->
	L = [1,2,3,4,5,6,7,8,9,10],
	check(L),
	check2(L, []).

check(L) -> lists:filter(fun(X) -> X rem 2 =:= 0 end, L).

check2([], A) -> A;
check2([H | T], A) ->
	C = lists:filter(fun(X) -> (X + H) rem 3 =:= 0 end, T),
	C1 = [{H, X, H / X} || X <- C],
	check2(T, lists:append(A, C1)).

In my actual code I have 5 non-trivial filters and I'm trying at all 
costs to avoid duplication of tests, The example code below exactly 
specifies the issue (without the detail). i.e. for members of the list 
data that pass the initial check (in this case they are even numbers), 
perfom check2... check2 requires that each candidate element should be 
checked against all other members of the list (but not itself) and 
should not duplicate any pairwise test in the "expensive" check2...
(my data is actually records so avoiding the issue by using the fact 
that value of the list elements in the example is not going to help!)





More information about the erlang-questions mailing list