[Issue 6788] std.range.pairwise
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Mon Jan 5 05:03:20 PST 2015
https://issues.dlang.org/show_bug.cgi?id=6788
--- Comment #11 from bearophile_hugs at eml.cc ---
(In reply to bearophile_hugs from comment #10)
> .pairwise!2
pairwise takes only one argument sequence, so that comment was bad. The
template argument is used to denote how many items you want in your "pairs"
(where 2 is the default).
So:
[1, 2, 3, 4].pairwise
Or:
[1, 2, 3, 4].pairwise!2
should yield:
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
[1, 2, 3, 4].pairwise!3
should yield:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
Like in this lower level code:
void main() {
import std.stdio, std.range, std.algorithm;
auto a = [1, 2, 3, 4];
foreach (immutable i; 0 .. a.length)
foreach (immutable j; i + 1 .. a.length)
writefln("(%d, %d)", a[i], a[j]);
writeln;
foreach (immutable i; 0 .. a.length)
foreach (immutable j; i + 1 .. a.length)
foreach (immutable k; j + 1 .. a.length)
writefln("(%d, %d, %d)", a[i], a[j], a[k]);
}
--
More information about the Digitalmars-d-bugs
mailing list