Adjacent Pairs Range

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 12 03:39:21 PDT 2015


On Saturday, 12 September 2015 at 10:35:41 UTC, Bahman Movaqar 
wrote:
> On 09/12/2015 02:47 PM, "Nordlöw" wrote:
>> How do I most elegantly iterate all the adjacent pairs in an 
>> `InputRange` using Phobos?
>> 
>> Something like
>> 
>>     [1,2,3,4] => [(1,2), (2,3), (3,4)]
>
> That's call `collate`ing IIRC.
> A quick solution would be using `std.range.transposed`:
>
> auto a = [1,2,3,4];
> auto ll = [a, a[1..$]];
> transpose(ll); // returns [[1, 2], [2, 3], [3, 4], [4]]

InputRange please, not RandomAccessRanges ;)


More information about the Digitalmars-d-learn mailing list