Generalizing aggregate/3 by adding foldl semantics

The aggregation library supports count, sum, min, max, etc.

A generalization of this would be to use foldl/4 semantics. For example, the sum operator could be implemented by aggregate(foldl(plus), Goal, 0, Result).

It might also be useful to have a post-aggregation option – e.g., the setof operator could be implemented by foldl(app) + sort/2 (where app/3 is defined: app(X,Xs,[X|Xs])). (It’s not clear to me how to efficiently define the bagof operator; a reverse_bagof is easy, using this predicate: reverse_append(X,Xs,[X|Xs]).)

1 Like