More on "Component Programming"

H. S. Teoh hsteoh at quickfur.ath.cx
Mon May 27 19:35:36 PDT 2013


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.


T

-- 
It only takes one twig to burn down a forest.


More information about the Digitalmars-d mailing list