On 1/23/15 10:29 AM, H. S. Teoh via Digitalmars-d wrote:
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.

Clever! Or, conversely, I'm not that bright! Yes, this is awesome. Probably the actual name "aggregate" should be defined even with that trivial implementation to help folks like me :o). -- Andrei

Reply via email to