[WORK] groupBy is in! Next: aggregate
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 23 10:29:13 PST 2015
On Fri, Jan 23, 2015 at 10:08:30AM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> 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.
Unfortunately it doesn't work in pure/@safe/nothrow code because of
limitations in the current RefCounted implementation.
[...]
> 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?
[...]
Isn't that just a simple matter of defining aggregate() in terms of
map() and reduce()? Working example:
import std.algorithm.comparison : max;
import std.algorithm.iteration;
import std.stdio;
auto aggregate(alias func, RoR)(RoR ror) {
return ror.map!(reduce!func);
}
void main() {
[293, 453, 600, 929, 339, 812, 222, 680, 529, 768]
.groupBy!(a => a & 1)
.aggregate!max
.writeln;
}
Output is as expected.
T
--
Verbing weirds language. -- Calvin (& Hobbes)
More information about the Digitalmars-d
mailing list