Mixin - to get to the content-type `MapResult!(__lambda1, int[]).MapResult`

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 29 23:50:06 PDT 2015


On 05/29/2015 06:07 PM, Dennis Ritchie wrote:

 > Hi,
 > This code prints the arrays:
 > [5]
 > [6]
 > [7]
 >
 > import std.stdio, std.algorithm;
 >
 > static int idx;

Do you want to share that for the first element of every two-element 
array or do you want to start from 0 for every range?

 > void walk(R)(R range) {
 >      while (!range.empty) {
 >          range.front;
 >          range.popFront;
 >          ++idx;
 >      }
 > }

As a reminder, there is also the recent 'each', which is eager as well.

 > void main() {
 >      [5, 6, 7].map!(a => [a].writeln).walk;
 > }
 >
 > How should I apply mixins to `range.front` to the program to print the
 > arrays:
 > [0, 5]
 > [1, 6]
 > [2, 7]

Unless mixin required for another reason, there are other ways of 
achieving the same.

 > Ie I want to get something like this:
 >
 > void walk(R)(R range) {
 >      while (!range.empty) {
 >          // mixin(`[idx ~ "mixin("range.front")"[1 .. $]);`);
 >          range.popFront;
 >          ++idx;
 >      }
 > }
 >
 > Can I do this?
 >
 > -----
 > Thank you for the function `walk` Mark Isaacson of presentation DConf 
2015:
 > http://dconf.org/2015/talks/isaacson.pdf

The following program produces the desired output twice. The first one 
starts from 0 for the index, the second one shares the index as in your 
code.

import std.stdio, std.algorithm, std.range;

void main() {
     zip(sequence!"n", [5, 6, 7])
         .map!(a => [a.expand])
         .each!writeln;

     static int idx;

     [5, 6, 7]
         .map!(a => [idx++, a])
         .each!writeln;
}

Ali



More information about the Digitalmars-d-learn mailing list