ets:foldl/3
对 ETS 数据遍历循环操作
用法:
foldl(Function, Acc0, Tab) -> Acc1
内部实现:
-spec foldl(Function, Acc0, Tab) -> Acc1 when Function :: fun((Element :: term(), AccIn) -> AccOut), Tab :: tab(), Acc0 :: term(), Acc1 :: term(), AccIn :: term(), AccOut :: term(). foldl(F, Accu, T) -> ets:safe_fixtable(T, true), First = ets:first(T), try do_foldl(F, Accu, First, T) after ets:safe_fixtable(T, false) end. do_foldl(F, Accu0, Key, T) -> case Key of '$end_of_table' -> Accu0; _ -> do_foldl(F, lists:foldl(F, Accu0, ets:lookup(T, Key)), ets:next(T, Key), T) end.
对 ETS 数据遍历循环操作,对数据遍历的规则跟 lists:foldl/3 一样。
Tab = ets:new(ets_tab, [named_table, set]), ets:insert(Tab, [{a, 1}, {b, 2}, {c, 3}, {d, 4}, {e, 5}]), ets:foldl(fun({_Key, Val}, AccVal) -> AccVal + Val end, 0, Tab).