Foreach over tuple of arrays?

Philippe Sigaud philippe.sigaud at gmail.com
Mon Aug 8 12:09:45 PDT 2011


Hi Sean,

> This doesn't work when passing a delegate as func, however. Is there a way to
> circumvent this?

I tried this with a delegate and a function template, it seems to work:


import std.typecons;

void bar(T)(ref T elem)
{
   elem = elem+1;
}

void main()
{
    auto data = tuple([0,1,2,3], [3,2,1], [0,1]);

    auto foo = (ref int i){ i = i * i;};

    writeln(data);

    foreach(index, range; data.expand)
        foreach(ref element; range)
            foo(element);

    writeln(data);

    foreach(index, range; data.expand)
        foreach(ref element; range)
            bar(element);

    writeln(data);
}


data.expand is only needed to get access to the underlying expression
template in the 'data' tuple.
Your own ArrayTuple!(T) may have other way to do that.


More information about the Digitalmars-d-learn mailing list