Adjacent Pairs Range

Bahman Movaqar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 12 03:35:38 PDT 2015


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]]

Though you have to take care of the dangling last element yourself.

-- 
Bahman Movaqar


More information about the Digitalmars-d-learn mailing list