On Fri, Jan 23, 2015 at 10:29:13AM -0800, H. S. Teoh via Digitalmars-d wrote:
> On Fri, Jan 23, 2015 at 10:08:30AM -0800, Andrei Alexandrescu via 
> Digitalmars-d wrote:
[...]
> > 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.
[...]

Here's a working variadic implementation:

        import std.algorithm.comparison : max, min;
        import std.algorithm.iteration;
        import std.stdio;
        
        template aggregate(funcs...) {
                auto aggregate(RoR)(RoR ror) {
                        return ror.map!(reduce!funcs);
                }
        }
        
        void main() {
            [293, 453, 600, 929, 339, 812, 222, 680, 529, 768]
                 .groupBy!(a => a & 1)
                 .aggregate!(max,min)
                 .writeln;
        }

Output (kinda ugly, but it works):

        [Tuple!(int, int)(453, 293), Tuple!(int, int)(600, 600), Tuple!(int, 
int)(929, 339), Tuple!(int, int)(812, 222), Tuple!(int, int)(529, 529), 
Tuple!(int, int)(768, 768)]


Of course, it will require a little more polish before merging into
Phobos, but the core implementation is nowhere near the complexity of
groupBy.


T

-- 
The best compiler is between your ears. -- Michael Abrash

Reply via email to