Transform a sorted range to a range of ranges of equal elements

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 1 17:39:34 PST 2014


On Tue, Dec 02, 2014 at 01:03:12AM +0000, Tobias Pankrath via Digitalmars-d-learn wrote:
> >
> >Phobos git HEAD has a new range adaptor called groupBy that does what
> >you want:
> >
> >	assert([1,1,2,2,2,3,4,4].groupBy!((a)=>a).equal(
> >		[[1,1], [2,2,2], [3], [4,4]]
> >	))
> >
> >
> >T
> 
> Thanks! I wonder if this works with all input ranges. As I see it,
> every implementation will have to iterate the original range twice (if
> fully consumed). One iteration by the subranges and one to move
> subrange range forward. It only skimmed the code, but I'd thought that
> would at least require forward ranges or a buffer.

The current implementation actually does work with input ranges (i.e.,
non-forward), but with the caveat that advancing the parent range will
also consume the subrange, so you have to process the subrange before
advancing the parent range. It is thus single-pass for non-forward input
ranges, as required (otherwise a forward range would be necessary).


T

-- 
Not all rumours are as misleading as this one.


More information about the Digitalmars-d-learn mailing list