More on "Component Programming"

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 27 19:01:32 PDT 2013


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.

Andrei


More information about the Digitalmars-d mailing list