[WORK] groupBy is in! Next: aggregate

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 23 10:08:30 PST 2015


So H.S. Teoh awesomely took 
https://github.com/D-Programming-Language/phobos/pull/2878 to 
completion. We now have a working and fast relational "group by" facility.

See it at work!

----
#!/usr/bin/rdmd

void main()
{
     import std.algorithm, std.stdio;
     [293, 453, 600, 929, 339, 812, 222, 680, 529, 768]
         .groupBy!(a => a & 1)
         .writeln;
}
----

[[293, 453], [600], [929, 339], [812, 222, 680], [529], [768]]

The next step is to define an aggregate() function, which is a lot 
similar to reduce() but works on ranges of ranges and aggregates a 
function over each group. Continuing the previous example:

     [293, 453, 600, 929, 339, 812, 222, 680, 529, 768]
         .groupBy!(a => a & 1)
         .aggregate!max
         .writeln;

should print:

[453, 600, 929, 812, 529, 768]

The aggregate function should support aggregating several functions at 
once, e.g. aggregate!(min, max) etc.

Takers?


Andrei


More information about the Digitalmars-d mailing list