More on "Component Programming"

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


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


More information about the Digitalmars-d mailing list