Suggestion: New table iterators

Bjorn Gustavsson <>
Thu Oct 19 11:29:47 CEST 2000


Robert Virding <> writes:

> Bjorn Gustavsson <> writes:
> >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.
> 
> Definitely YES!  But I am not really convinced you need l/r.  Perhaps a
> just fold/3 as well?  I generally think it is a good idea to add
> iterators over to make it easier and, more importantly, clearer what 
> you are doing.

Ok, maybe order isn't that important. It would definitely be simpler
and clearer having just

	ets:fold(Fun, Acc, Table)
	dets:fold(Fun, Acc, Table)
	mnesia:fold(Fun, Acc, Table)

It would also avoid some of the ordering problems Ulf mentioned in his
mail (bug in ets:last/1, Mnesia transactions).

Is anyone against implmentering fold/3 instead of fold[rl]/3?

> 
> >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?
> 
> I definitely think that map and filter should return a modified table to
> keep the general semantics of mapping and filtering consistent.  Not
> returning a new table is OK here as the semantics of tables is
> destructive.  With those semantics it would OK to add those functions
> now but with relatively naive implementations.  At least filter.
> 
> 	Robert
> 
> -- 
> Robert Virding                          Tel: +46 (0)8 545 55 017
> Alteon Web Systems                      Email: 
> S:t Eriksgatan 44                       WWW: http://www.bluetail.com/~rv
> SE-112 34 Stockholm, SWEDEN
> "Folk säger att jag inte bryr mig om någonting, men det skiter jag i".
> 
> 

-- 
Björn Gustavsson            Ericsson Utvecklings AB
      ÄT2/UAB/F/P
			    BOX 1505
+46 8 727 56 87 	    125 25 Älvsjö



More information about the erlang-questions mailing list