groupBy predicates: unary, binary, or both?

bearophile bearophileHUGS at lycos.com
Sun Dec 29 02:27:27 PST 2013


Andrei Alexandrescu:

> which is somewhat important; "group by" is a powerful operation.

I agree, it's a quite commonly useful operation.


> So I was thinking to allow both cases, with the understanding 
> that grouping by unary predicates uses "==" for comparison 
> whereas grouping by binary predicates looks at adjacent 
> elements to figure out group membership. That approach would, 
> however, preclude the use of string lambdas (because deducing 
> arity for string lambdas is possible, but quite unwieldy).

In similar cases the simplest solution is to have two functions 
with different (but similar) names.


> However, that makes life a bit tougher for the algorithm - it 
> must only compare adjacent elements only.

But another common need is to group by equivalent classes using 
an associative array. Both C# and Clojure have this functionality 
(on LINQ and in the functions toolkit). This means that a 
"hashGroup" builds an associative array inside and returns it.


See also:
http://d.puremagic.com/issues/show_bug.cgi?id=5968
http://d.puremagic.com/issues/show_bug.cgi?id=9842

Bye,
bearophile


More information about the Digitalmars-d mailing list