[Issue 6788] std.range.pairwise

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jan 5 04:23:37 PST 2015


https://issues.dlang.org/show_bug.cgi?id=6788

--- Comment #10 from bearophile_hugs at eml.cc ---
Elsewhere I have suggested to add to cartesianProduct an optional template
argument that specifies the "repetitions", just like the Python version of
product:

https://docs.python.org/2/library/itertools.html#itertools.product

Python code:

product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy

product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111


Equivalently for the D code (the "repeat" argument "3" needs to be a template
argument because cartesianProduct yields tuples, that must have their structure
known at compile-time):

cartesianProduct!3([0, 1]) => 000 001 010 011 100 101 110 111

Now I've seen the same optional repeat template argument is useful for pairwise
too, because if you are inside a UFCS chain and you want to perform a pairwise
on the sequence items, you don't need to break the chain:


auto seq = 10.iota
           .map!(...)
           .filter!(...);

auto result = pairwise(seq, seq)
              .filter!(...);


Becomes a single nicer chain:

auto result = 10.iota
              .map!(...)
              .filter!(...)
              .pairwise!2
              .filter!(...);


Such use case "pairwise(seq,seq)" with repeated argument is probably common.

--


More information about the Digitalmars-d-bugs mailing list