Suggestion: New table iterators
Ulf Wiger
etxuwig@REDACTED
Thu Oct 19 10:55:17 CEST 2000
Also, it would be nice to perform a list comprehension on an ets
table.
Using my hack of sys_pre_expand, it would have been easily
accomplished by just adding a function
ets:tab2lazy_list(Tab) ->
K = ets:first(Tab),
[Obj] = ets:lookup(Tab, K),
[K|fun() -> next_in_tab(Tab, ets:next(Tab, K)) end].
next_in_tab(Tab, '$end_of_table') -> [];
next_in_tab(Tab, Key) ->
[Obj] = ets:lookup(Tab, Key),
[Obj | fun() -> next_in_tab(Tab, ets:next(Tab, K)) end].
(ignoring the complication of bag tables for demo purposes.)
Oh well. I'm sure it's not that hard anyway.
/Uffe
On 19 Oct 2000, Bjorn Gustavsson wrote:
>I suggest that we add
>
> ets:foldl(Fun, Acc, Table)
> ets:foldr(Fun, Acc, Table)
>
> dets:foldl(Fun, Acc, Table)
> dets:foldr(Fun, Acc, Table)
>
> mnesia:foldl(Fun, Acc, Table)
> mnesia:foldr(Fun, Acc, Table)
>
>for iteration over tables.
>
>The difference between the foldl/3 and foldr/3 is the order in which the
>table elements are accessed. foldl/3 accesses from the first element to
>the last, while foldr/3 accesses the element from last to to first.
>Both functions are tail-recursive. The order is important only for ordered tables.
>
>There is no need for map/2 or filter/3 functions, because they can easily
>be simulated with the fold functions:
>
> my_tab2list(T) ->
> ets:foldr(fun (E, Acc) -> [E|Acc] end, [], T).
>
>Besides, the reasonable semantics for map/2 and filter/3 would be that
>they modified the ets table or returned a new ets table, something we
>we are no prepared to implement now, but might be implemented in a future version.
>
>Any comments?
>
>/Björn
>
--
Ulf Wiger tfn: +46 8 719 81 95
Strategic Product & System Management mob: +46 70 519 81 95
Ericsson Telecom AB, Datacom Networks and IP Services
Varuvägen 9, Älvsjö, S-126 25 Stockholm, Sweden
More information about the erlang-questions
mailing list