chunks equivalent with unpacking?

bearophile bearophileHUGS at lycos.com
Wed Jul 31 11:19:01 PDT 2013


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

Silly me, that Iota is not needed:


import std.stdio : writeln;
import std.range: chunks, Chunks, iota;
import std.string: format, join;
import std.typecons: tuple;
import std.algorithm: map;

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

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

     void popFront() { cr.popFront; }

     @property front() {
         mixin("return tuple(" ~
               n.iota.map!(i => format("cr.front[%d]", i)).join(", 
") ~
               ");");
     }
}

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;
}


(It still doesn't work, but it doesn't work in a better way).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list