Getting equivalent elements in a range/array

bearophile bearophileHUGS at lycos.com
Sun May 8 01:54:33 PDT 2011


Andrej M.:

> I want to turn this:
> auto arr = [1, 1, 2, 3, 4, 4];
> 
> into this:
> auto arr2 = [[1, 1], [2], [3], [4, 4]];
> 
> I want an array of arrays of the same elements. Lazy or not, I don't care.

Currently if you use group like this:
writeln(arr.group());

You get:
[Tuple!(int,uint)(1, 2), Tuple!(int,uint)(2, 1), Tuple!(int,uint)(3, 1), Tuple!(int,uint)(4, 2)]

Andrei has recently said he wants to modify group() to make it work more like python itertools.groupby(), so it will do what you want (the duplicated items too will be lazy, as in Python).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list