On Mon, May 27, 2013 at 9:53 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
On Mon, May 27, 2013 at 09:24:28PM -0700, Timothee Cour wrote:
> > 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:
[...]

The problem with allowing the user to specify order is that when one or
more of the input ranges are infinite, the order of traversal is much
less flexible, and it may not be possible to satisfy the requested
order.

Explicit is better than implicit. The user should request the order, and the compiler can statically disallow lexicographic_depth/antilexicographic_depth when at least one of the ranges is infinite. 
 

The order that Andrei suggested is what's currently used for two
infinite ranges, though when finite ranges are involved I chose a
slightly simpler implementation. The order bearophile proposed in issue
9878 is different from what Andrei describes, at any rate.

What's the typical output order of cartesian products in other languages
/ libraries? It seems nobody can agree on what the order should be. :-/


python uses itertools.product which is lexicographic_depth.
Like you say, no-one can agrees what the order should be, so let's leave it up to user through a template. Sounds like a no-brainer to me. There are use cases for each order I mentioned.
 

T

--
People demand freedom of speech to make up for the freedom of thought
which they avoid. -- Soren Aabye Kierkegaard (1813-1855)