[erlang-questions] testing a list against an ets table

Doug Edmunds <>
Fri Jun 20 01:00:57 CEST 2008

The goal is determine if any tuples in the List are not
in the Table (and if so, identify which are missing).

I wrote two functions, one which uses ets:match_object/2 and
the other which uses lists:member/2.

Function parameters:
List is a list of tuples [{a,b}, {c,d,e}, {a,d}, ...].
Table is an open ets table of type 'bag', which means
there can be more than one item with the same key in
the table.

Which of these methods is preferable, and why?

%% a quick display utility function
io(X) -> io:format("~p~n",[X]).

findlist1(List,Table) ->
    R1 = [E || E <- List, length(ets:match_object(Table, E)) == 0],
    io(R1),           %% elements not found in table
    io(length(R1)).   %% if length(R1) == 0, all are in list

findlist2(List, Table) ->
    AsList = ets:tab2list(Table),
    R1 = [E || E <- List, not lists:member(E,AsList)],
    io(R1),   %% same as above

length(R1) could be turned into true/false thus:

case length(R1) of

      0 -> true;  % all in table
      _Other -> false

What are some other alternatives?

Also, I would appreciate suggestions on
how to short-circuit the evaluation
(stopping as soon as one element of the
list is not found in the table).


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080619/a70638d0/attachment.html>

More information about the erlang-questions mailing list