www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Transform a sorted range to a range of ranges of equal elements

reply "Tobias Pankrath" <tobias pankrath.net> writes:
Basically I need std.algorithm.uniq or std.algorithm.group, but 
instead of a single element or an element and a number I want 
ranges that each contain consecutive elements considered equal.

Example: [1,1, 2,2,2,3,4,4] -> [1, 1], [2,2,2], [3], [4,4].

Let's call this uniqRange. This way std.algorithm.uniq could be 
implemented as

auto uniq(R, pred)(R input)
{
     return uniqRange!pred(R).map!(r => r.front));
}

Is there any elegant way to do this with using phobos? Or do I 
need to write my own range?
Dec 01 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Mon, Dec 01, 2014 at 06:37:13PM +0000, Tobias Pankrath via
Digitalmars-d-learn wrote:
 Basically I need std.algorithm.uniq or std.algorithm.group, but
 instead of a single element or an element and a number I want ranges
 that each contain consecutive elements considered equal.
 
 Example: [1,1, 2,2,2,3,4,4] -> [1, 1], [2,2,2], [3], [4,4].
 
 Let's call this uniqRange. This way std.algorithm.uniq could be
 implemented as
 
 auto uniq(R, pred)(R input)
 {
     return uniqRange!pred(R).map!(r => r.front));
 }
 
 Is there any elegant way to do this with using phobos? Or do I need to
 write my own range?
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 -- You have to expect the unexpected. -- RL
Dec 01 2014
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
 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.
Dec 01 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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.
Dec 01 2014