[Issue 7128] Cartesian product of ranges

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jan 8 01:22:57 PST 2015


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

--- Comment #20 from bearophile_hugs at eml.cc ---
Thinking some more about this. Currently the "repeat" argument needs to be a
template argument:

cartesianProduct!2([0, 1]) ==> tuple(0,0), tuple(0,1), tuple(1,0), tuple(1,1)

But it's probably better to change everything, so now I think it's better to
have a differently named function with a different API. Something like:

cartesianPower!true([0, 1], 2) => [0,0], [0,1], [1,0], [1,1]

cartesianPower!false([0, 1], 2) => [0,0], [0,1], [1,0], [1,1]

This means it yields arrays instead of tuples, and the power argument is a
run-time argument. The boolean template argument is true by default, and it's
named doCopy, if it's true every output array is a new array, otherwise if
doCopy is false, it's always the same array with different contents.

cartesianPower works efficiently, it's not recursive.

If you like this idea, I can close down this issue, and open a new enhancement
request for cartesianPower.

--


More information about the Digitalmars-d-bugs mailing list