Suggestion: New table iterators

Robert Virding <>
Thu Oct 19 11:27:47 CEST 2000

Ulf Wiger <> writes:
>Also, it would be nice to perform a list comprehension on an ets
>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.)

This is relatively easy to do in sys_pre_expand.  On idea I had is to
use list comprehensions with special forms for generators, for example

    R <- ets(foo)
    R <- mnesia(foo)

which would extract elements from ets/mnesia tables.  The implementation
would be very similar to what you have written and generate the table
elements one at a time and pass them into the lc. It would of course be 
possible to mix tabel and list generators.

I prefer this way to generating lazy lists.  But it is harder to pass 
an accumulator along.

The problem with using lc is that you are explicitly building a list of 
values, you are going Table -> List.  This abuses the lc notion, 

I think the fold function is MUCH better as it can do all the LC can, 
plus much more.  The intent is also clearer.


Robert Virding                          Tel: +46 (0)8 545 55 017
Alteon Web Systems                      Email: 
S:t Eriksgatan 44                       WWW:
SE-112 34 Stockholm, SWEDEN
"Folk säger att jag inte bryr mig om någonting, men det skiter jag i".

More information about the erlang-questions mailing list