proper groupBy

Laeeth Isharc via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 23 19:22:40 PST 2015


On Friday, 23 January 2015 at 20:28:32 UTC, Andrei Alexandrescu 
wrote:
> On 1/23/15 12:19 PM, Ary Borenszweig wrote:
>> In most languages group by yields a tuple of {group key, group 
>> values}.
>
> Interesting, thanks. Looks like we're at a net loss of 
> information with our current approach.
>
> @quickfur, do you think you could expose a tuple with "key" and 
> "values"? The former would be the function value, the latter 
> would be what we offer right now.
>
> That would apply only to the unary version of groupBy.
>
>
> Andrei

groupby hack below ?  I haven't yet read the source code and 
don't feel I understand ranges deeply enough to know if this will 
work in the general case.  But it at least works for the example 
(I think).


Laeeth.

#!/usr/bin/rdmd


void main()
{
     import std.algorithm, std.stdio, std.range;
     auto index=[293, 453, 600, 929, 339, 812, 222, 680, 529, 768];
     auto vals=[	1,		2,	3,	4,	5,		6,	7,	8,	9,		10];
     auto zippy=zip(index,vals);

     zippy.groupBy!(a=> a[0] & 1)
         .writeln;
}

[root at fedorabox test]# ./groupby
[[Tuple!(int, int)(293, 1), Tuple!(int, int)(453, 2)], 
[Tuple!(int, int)(600, 3)], [Tuple!(int, int)(929, 4), 
Tuple!(int, int)(339, 5)], [Tuple!(int, int)(812, 6), Tuple!(int, 
int)(222, 7), Tuple!(int, int)(680, 8)], [Tuple!(int, int)(529, 
9)], [Tuple!(int, int)(768, 10)]]


More information about the Digitalmars-d mailing list