> Done, turns out the fix was trivial, just swapping two static ifs:
https://github.com/D-Programming-Language/phobos/pull/1314

This isn't what Andrei had in mind in his post above:

> I'm disappointed cartesianProduct works that way; I should have caught that during the code review. A better iteration order would have spanned the lower position in both ranges first, i.e. create squares of increasing side in the 2D space.

I would suggest an additional template parameter to specify the order of iteration:
for example an enum Order {lexicographic_depth, lexicographic_breadth,antilexicographic_depth, antilexicographic_breadth}.
The naming is horrible but you get the idea:
cartesianProduct!(order)(['a','b','c'],[1,2,3]))
order=Order.lexicographic_depth:
a1 a2 a3 b1 b2 b3 c1 c2 c3
order=Order. lexicographic_breadth:
a1 b1 a2 c1 b2 a3 c2 b3 c3
ie , in 2D, it follows diagonals x+y=k, with k=0,1,2,... and 0<=x<length(x range), 0<=y<length(y range)
order=Order.antilexicographic_depth:
a1 b1 c1 a2 b2 c2 a3 b3 c3
order=Order.antilexicographic_breadth:
a1 a2 b1 a3 b2 c1 b3 c2 c3


On Mon, May 27, 2013 at 7:58 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
On Mon, May 27, 2013 at 07:35:36PM -0700, H. S. Teoh wrote:
> On Mon, May 27, 2013 at 10:01:32PM -0400, Andrei Alexandrescu wrote:
> > On 5/27/13 5:36 PM, bearophile wrote:
> > >This simple example shows the difference:
> > >
> > >import std.stdio, std.algorithm;
> > >void main() {
> > >auto data = [1, 2, 3, 4];
> > >foreach (xy; cartesianProduct(data, data))
> > >writeln(xy);
> > >}
> > >
> > >
> > >Generates the tuples:
> > >(1, 1)
> > >(2, 1)
> > >(3, 1)
> > >(4, 1)
> > >(1, 2)
> > >(2, 2)
> > >(3, 2)
> > >(4, 2)
> > >(1, 3)
> > >(2, 3)
> > >(3, 3)
> > >(4, 3)
> > >(1, 4)
> > >(2, 4)
> > >(3, 4)
> > >(4, 4)
> >
> > I'm disappointed cartesianProduct works that way; I should have
> > caught that during the code review. A better iteration order would
> > have spanned the lower position in both ranges first, i.e. create
> > squares of increasing side in the 2D space.
>
> This is not too hard to change; it's just a matter of swapping left-
> and right- recursion in the templates. I'll see if I can cook up an
> pull request in a bit.
[...]

Done, turns out the fix was trivial, just swapping two static ifs:

https://github.com/D-Programming-Language/phobos/pull/1314


T

--
I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike Dresser