### Re: help on cartesianProduct()

```
On Sunday, 11 December 2016 at 23:07:16 UTC, Era Scarecrow wrote:
On Sunday, 11 December 2016 at 18:05:19 UTC, Era Scarecrow
wrote:

On Sunday, 11 December 2016 at 16:34:38 UTC, Orut wrote:
I need to be able to vary the number of ranges to feed into
cartesianProduct() at run time.

Hmmm... what kind of ranges? Are they going to be arrays? Or
something else?

Well, with the assumption of using arrays (since you kinda need
a length for it to work) I've thrown together a quick struct
that does the job. Not fully tested, but will do the same thing
with a varying number of inputs.

[code]
struct MultiCart(T) {
T[][] data;
int iteration;
int max;

this(T[][] _d, int iter=0) {
data = _d;
iteration = iter;

max = 1;
foreach(a; _d) {
if (a.length)
max *= a.length;
}
}

T[] front() {
int i = iteration;
T[] val;

foreach(d; data) {
if (d.length) {
val ~= d[i % d.length];
i /= d.length;
}
}

return val;
}

void popFront() {
iteration++;
}

bool empty() {
return iteration >= max;
}
}

unittest {
import std.stdio;
alias CartInt = MultiCart!int;

int[] a=[1,2,3],b=[4,5],c=[6,7];
foreach(x; CartInt([a,b,c]))
writeln(x);

foreach(x; CartInt([a,b]))
writeln(x);
}
[/code]

The code works beautifully! Thank you very much. Will certainly
acknowledge you as source of this code if this becomes of part of
a bigger project. I hope others searching this forum would also
discover this code.

```

### help on cartesianProduct()

```

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.

```

### Re: faster "stringification"

```On Sunday, 11 December 2016 at 02:46:58 UTC, Nicholas Wilson
wrote:

join performs allocations which is probably the reason for its
slowness. There is joiner (in std.algorithm.iterations) that
lazily performs the join, (though in the case of this
"benchmark" will be cheating because you don't do anything with
the result, print it to get a more fair comparison) avoiding
allocation.

Thanks, Stefan and Nicholas. I think joiner did the trick (for
50M iterations, ~2s for D, ~17s for Python).

```

### faster "stringification"

```D nub here. I have a Python script that I'd like to implement in
D. For certain parts, the D equivalent was slower than Python's.
For example,

Python code:

#dummy code
s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg",
"k", "jds", "yd"];

for i in range(1000):  # a lot of array to string conversions
'-'.join(s)# not assigning this to a variable to simplify
comparison

D code:

import std.stdio;
import std.array;

void main(string[] args){
string[] s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro",
"bc", "sg", "k", "jds", "yd"];
for(int i; i<10_000_000; i++) s.join("-"); //see Python