help on cartesianProduct()

Orut via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 11 08:34:38 PST 2016


Am trying to port some Python code to D and I got stumped on the 
use of cartesianProduct() from std.algorithm.setops. In Python, 
the same functionality is implemented by product() in the 
itertools module. I need to be able to vary the number of ranges 
to feed into cartesianProduct() at run time. In Python, this is 
possible because I can dynamically construct a list of lists, 
then unpack this list using the unpacking operator when it is fed 
as argument to product(). In D, as far as I can tell (D nub 
here), I can't unpack arrays (no expand property), and I can't 
dynamically change the content of tuples (fixed at compile time). 
A possible hack is to create different versions of the function 
call (each with a fixed number of arguments), say cases under a 
switch, but this gets ugly if I anticipate a large number of 
possible scenarios. Would appreciate any idea. Thanks.


More information about the Digitalmars-d-learn mailing list