merging a group result

Alex sascha.orlov at gmail.com
Mon Jul 23 11:49:58 UTC 2018


Hi all,
I'm looking for a d-ish way to solve a basic 
"split-apply-combine" workflow. The idea is described (and 
solved) here:

https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum

So, given a structure with some fields, say

´´´
struct S
{
	string s;
	int i;
}
´´´

I create an array of them, like

´´´
void main()
{
         import std.experimental.all;

	S[] sarr;
	sarr.length = 6;
	sarr[0].s = "a";
	sarr[1].s = "a";
	sarr[2].s = "b";
	sarr[3].s = "b";
	sarr[4].s = "c";
	sarr[5].s = "c";
	sarr[0].i = 1;
	sarr[1].i = 2;
	sarr[2].i = 4;
	sarr[3].i = 8;
	sarr[4].i = 16;
	sarr[5].i = 32;
	auto res = sarr.group!((a, b) => a.s == b.s);
         //writeln(res);
}
´´´

I'm also able to group them by a field, see last line.

But now the problems begin:
- The group operation tries to use the structure itself as a key, 
despite I provide a custom binary predicate.
- I could ignore the fact above, but how, given the result of the 
grouping operation I can merge by some function (like sum) the 
group results?

At this moment, I assume, that I'm approaching the problem from 
the wrong end, and simply don't see something trivial... Anyway, 
does anybody has a hint for me?


More information about the Digitalmars-d-learn mailing list