pairwise

All Unordered Element Pairs (2-Element Subsets) of a Range.

TODO Add template parameter to decide if .array should be used internally.

pairwise
(
R
)
(
R r
)
if (
isInputRange!R
)

Examples

test RandomAccessRange input

import std.algorithm: equal, filter;
import std.typecons: Tuple;

assert((new int[0]).pairwise.empty);
assert([1].pairwise.empty);

alias T = Tuple!(int, int);
assert(equal([1, 2].pairwise,
             [T(1, 2)]));
assert(equal([1, 2, 3].pairwise,
             [T(1, 2), T(1, 3), T(2, 3)]));
assert(equal([1, 2, 3, 4].pairwise,
             [T(1, 2), T(1, 3), T(1, 4),
              T(2, 3), T(2, 4), T(3, 4)]));

test ForwardRange input

import std.algorithm: equal, filter;
import std.array : array;

auto p = [1].filter!"a < 4".pairwise;
assert(p.empty);

assert(equal(p.array,
             [1].pairwise.array));

assert(equal([1, 2, 3, 4].filter!"a < 4".pairwise,
             [1, 2, 3].pairwise));

See Also

Meta