<div>> Done, turns out the fix was trivial, just swapping two static ifs:<br>> <a href="https://github.com/D-Programming-Language/phobos/pull/1314" target="_blank">https://github.com/D-Programming-Language/phobos/pull/1314</a></div>
<div><br></div>This isn't what Andrei had in mind in his post above:<div><br><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">> 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.</span></div>
<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">I would suggest an additional template parameter to specify the order of iteration:</font></div><div>
<font color="#222222" face="arial, sans-serif">for example an enum Order {</font><font color="#222222" face="arial, sans-serif">lexicographic_depth, </font><span style="color:rgb(34,34,34);font-family:arial,sans-serif">lexicographic_breadth,anti</span><font color="#222222" face="arial, sans-serif">lexicographic_depth, anti</font><span style="color:rgb(34,34,34);font-family:arial,sans-serif">lexicographic_breadth</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">}.</span></div>
<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif">The naming is horrible but you get the idea:</span></div><div>cartesianProduct!(order)(['a','b','c'],[1,2,3]))</div><div><div>order=<span style="color:rgb(34,34,34);font-family:arial,sans-serif">Order.</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">lexicographic_depth:</span></div>
<div>a1 a2 a3 b1 b2 b3 c1 c2 c3</div></div><div><div>order=<span style="color:rgb(34,34,34);font-family:arial,sans-serif">Order.</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif"> lexicographic_breadth</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">:</span></div>
<div>a1 b1 a2 c1 b2 a3 c2 b3 c3</div></div><div>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)</div><div><div>order=<span style="color:rgb(34,34,34);font-family:arial,sans-serif">Order.anti</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">lexicographic_depth</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">:</span></div>
<div>a1 b1 c1 a2 b2 c2 a3 b3 c3</div></div><div><div>order=<span style="color:rgb(34,34,34);font-family:arial,sans-serif">Order.anti</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">lexicographic_breadth</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif">:</span></div>
</div><div><div>a1 a2 b1 a3 b2 c1 b3 c2 c3</div><div></div></div><div><br></div><div><br><div class="gmail_quote">On Mon, May 27, 2013 at 7:58 PM, H. S. Teoh <span dir="ltr"><<a href="mailto:hsteoh@quickfur.ath.cx" target="_blank">hsteoh@quickfur.ath.cx</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Mon, May 27, 2013 at 07:35:36PM -0700, H. S. Teoh wrote:<br>
> On Mon, May 27, 2013 at 10:01:32PM -0400, Andrei Alexandrescu wrote:<br>
> > On 5/27/13 5:36 PM, bearophile wrote:<br>
> > >This simple example shows the difference:<br>
> > ><br>
> > >import std.stdio, std.algorithm;<br>
> > >void main() {<br>
> > >auto data = [1, 2, 3, 4];<br>
> > >foreach (xy; cartesianProduct(data, data))<br>
> > >writeln(xy);<br>
> > >}<br>
> > ><br>
> > ><br>
> > >Generates the tuples:<br>
> > >(1, 1)<br>
> > >(2, 1)<br>
> > >(3, 1)<br>
> > >(4, 1)<br>
> > >(1, 2)<br>
> > >(2, 2)<br>
> > >(3, 2)<br>
> > >(4, 2)<br>
> > >(1, 3)<br>
> > >(2, 3)<br>
> > >(3, 3)<br>
> > >(4, 3)<br>
> > >(1, 4)<br>
> > >(2, 4)<br>
> > >(3, 4)<br>
> > >(4, 4)<br>
> ><br>
> > I'm disappointed cartesianProduct works that way; I should have<br>
> > caught that during the code review. A better iteration order would<br>
> > have spanned the lower position in both ranges first, i.e. create<br>
> > squares of increasing side in the 2D space.<br>
><br>
> This is not too hard to change; it's just a matter of swapping left-<br>
> and right- recursion in the templates. I'll see if I can cook up an<br>
> pull request in a bit.<br>
</div></div>[...]<br>
<br>
Done, turns out the fix was trivial, just swapping two static ifs:<br>
<br>
<a href="https://github.com/D-Programming-Language/phobos/pull/1314" target="_blank">https://github.com/D-Programming-Language/phobos/pull/1314</a><br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
T<br>
<br>
--<br>
I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike Dresser<br>
</font></span></blockquote></div><br></div></div>