Short version: is it safe to use ets:foldlETS to delete each record as it goes through them?
Suppose the ETS table is collecting information, and now it's time to process everything. The record is read from the table, used in some way, and then deleted. (Also, suppose the table privateis therefore not a concurrency problem.)
In another language with a similar data structure, you can use each loop by processing each record and then removing it from the / dict / map / whatever hash. However, the module etsdoes not have foreach, for example. listsdoes.
But this might work:
1> ets:new(ex, [named_table]).
ex
2> ets:insert(ex, {alice, "high"}).
true
3> ets:insert(ex, {bob, "medium"}).
true
4> ets:insert(ex, {charlie, "low"}).
true
5> ets:foldl(fun({Name, Adjective}, DontCare) ->
io:format("~p has a ~p opinion of you~n", [Name, Adjective]),
ets:delete(ex, Name),
DontCare
end, notused, ex).
bob has a "medium" opinion of you
alice has a "high" opinion of you
charlie has a "low" opinion of you
notused
6> ets:info(ex).
[...
{size,0},
...]
7> ets:lookup(ex, bob).
[]
Is this the preferred approach? Is this at least correct and error free?
, ets: foldl documentation , ETS foldl. , , , .
Erlang R14B set, , - Erlang . !