chunks equivalent with unpacking?

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed Jul 31 10:40:21 PDT 2013


std.range.chunks is useful for iterating through a range like so:

-----
import std.range;
import std.stdio : writeln;

void main()
{
    float[] arr = [
         0.0,   0.15, 0.0, 1.0,
         0.25, -0.25, 0.0, 1.0,
        -0.25, -0.25, 0.0, 1.0,
    ];

    foreach (xyzw; arr.chunks(4))
    {
        writeln(xyzw[0]);  // write 'x'
    }
}
-----

But instead of having to use indexing for each element in the chunk,
I'd like to use separate arguments instead, like so:

    // nicer API
    foreach (x, y, z, w; arr.chunks(4))
    {
        writeln(x);
    }

Is there a function in Phobos that does this?


More information about the Digitalmars-d-learn mailing list