chunks equivalent with unpacking?

Ali Çehreli acehreli at yahoo.com
Wed Jul 31 11:36:21 PDT 2013


On 07/31/2013 11:19 AM, bearophile wrote:
>> 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

Fixed magically :) by a line that should have no effect:

     pragma(msg, ElementType!(typeof(arr.tupleChunks!4)));

Of course one must also import ElementType:

import std.range: chunks, Chunks, iota, ElementType;

Ali



More information about the Digitalmars-d-learn mailing list