Ali Çehreli:

Once the element type is a tuple, 'tuple foreach' will expand them.

foreach (x, y, z, w; /* a range that returns tuple of four */)

That part of the D design is not yet formalized, it's a special case, and maybe it will need to be removed :-( It's a significant design mistake introduced recently in D.


This is a first nonworking try:


import std.range: chunks, Chunks;
import std.stdio : writeln;
import std.string: format, join;
import std.typecons: tuple;
import std.typetuple: TypeTuple;

template Iota(int stop) {
    static if (stop <= 0)
        alias TypeTuple!() Iota;
    else
        alias TypeTuple!(Iota!(stop-1), stop-1) Iota;
}

struct TupleChunks(size_t n, CR) {
    CR cr;

    @property bool empty() { return cr.empty; }

    void popFront() { cr.popFront; }

    @property front() {
        static string gen() {
            string[] result;
            foreach (i; Iota!n)
                result ~= format("cr.front[%d]", i);
            return result.join(", ");
        }

        mixin("return tuple(" ~ gen ~ ");");
    }
}

TupleChunks!(n, Chunks!R) tupleChunks(size_t n, R)(R r) {
    return typeof(return)(chunks(r, n));
}


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 (x, y, z, w; arr.tupleChunks!4)
        x.writeln;
}


Bye,
bearophile

Reply via email to