Empowering foreach with multiple aggregates

Janice Caron caron800 at googlemail.com
Sun Sep 9 03:49:47 PDT 2007


> foreach(ref T d ; dst)
>   d = 1;
>
> Becomes:
>
> dst.opApply((ref T d) {
>   d = 1;
>   return 0;
> });
>
> This is a problem if you want to do iteration over multiple aggregates;

Yes, I see. I had not realised that. Thanks.

I guess you could do it by creating some sort of hybrid collection -
but I don't know if that could be done at a language level (and if it
can't there'd be no point). By this I mean, given a collection C c,
and collection D d, where (c.length == d.length), generate a third
collection A a such that a[n] is a Tuple!(ref c[n], d[n]). Then you
could do:

 foreach(a; AggregatePair!(dst, src))
 {
     a[0] = a[1] + 1;
 }

But I don't have enough experience with Tuples to know if that's even
possible. I mean, AggregatePair!'s opApply() method would have to be
really clever.



> you'd essentially need to call two functions simultaneously, then have
> those call into a third function passing half of the arguments each.

Could this AggregatePair! idea do that? Or is that just too not possible?


> Adding this to D would probably require completely rethinking how custom
> aggregates are created, which isn't necessarily a bad thing, but
> certainly a difficult thing.

Oh well!
C'est la vie.



More information about the Digitalmars-d mailing list