# Accumulators..

rgowka1 rgowka1@REDACTED
Tue Oct 26 02:54:05 CEST 2010

Hi -

I am reading Joe's book and on page 75 there is a description of
accumulators. I really the idea of accumulators, especially the
advantage to avoid having to traverse a list multiple times.

My question is - Is there a way to generalize and extend the use of
accumulators? Joe divides a list of numbers into Evens and Odds by
using two empty lists are accumulators. Is there a way to extend this
approach to N buckets. For example, traverse a big list just one  -
lists:seq(1,math:pow(2,32)) and group them into 100 buckets like all
the numbers divisible by 2 in bucket 1, all the numbers divisible by 3
in bucket 2, all the numbers divisible by 5 in bucket 3... etc.

odds_and_evens_acc(L) -> odds_and_evens_acc(L, [], []).

odds_and_evens_acc([H|T], Odds, Evens) ->
case (H rem 2) of
1 -> odds_and_evens_acc(T, [H|Odds], Evens);
0 -> odds_and_evens_acc(T, Odds, [H|Evens])
end;
odds_and_evens_acc([], Odds, Evens) ->
{Odds, Evens}.

thanks.