On Friday, 23 January 2015 at 20:19:31 UTC, Ary Borenszweig wrote:
On 1/23/15 3:08 PM, Andrei Alexandrescu 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.

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

In most languages group by yields a tuple of {group key, group values}.

For example (Ruby or Crystal):

a = [1, 4, 2, 4, 5, 2, 3, 7, 9]
groups = a.group_by { |x| x % 3 }
puts groups #=> {1 => [1, 4, 4, 7], 2 => [2, 5, 2], 0 => [3, 9]}

In C# it's also called group by: http://www.dotnetperls.com/groupby

Java: http://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#groupingBy-java.util.function.Function-

SQL: http://www.w3schools.com/sql/sql_groupby.asp

So I'm not sure "groupBy" is a good name for this.

You are talking about two different functions here. group by and partition by. The function that has been implemented is often called partition by.

The best example I know of:

https://clojuredocs.org/clojure.core/group-by
https://clojuredocs.org/clojure.core/partition-by

Reply via email to