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

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 30 16:58:44 PDT 2015


On 05/30/2015 12:19 PM, Dennis Ritchie wrote:

First, unfortunately, I don't understand you completely. Sorry about 
that... :)

Second, you can do almost anything with string mixins: Write a function 
that returns the code as string and just mix it in. :) Debug with 
pragma(msg):

string makeCode(string name)
{
     return `int ` ~ name ~ ` = 42;`;
}

unittest
{
     assert(makeCode("foo") == "int foo = 42;");
}

 > I want to access the intermediate generation `range.front`. Is it
 > possible? :)

Regarding that, the intermediate range.front is already available right 
before the .walk part. You can do anything at that point. There was some 
proposals about a 'tap' algorithm that could be used for debugging 
purposes. Here is a quick implementation:

import std.stdio, std.algorithm;

static int idx;

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

struct Tap(alias func, R)
{
     R range;

     alias range this;

     @property auto front()
     {
         func(range.front);
         return range.front;
     }
}

auto tap(alias func, R)(R range)
{
     return Tap!(func, R)(range);
}

void main() {
     [5, 6, 7]
         .map!(a => [idx++, a])
         .tap!((a) { writeln(a[1..$]); })  /* <-- This can use the
                                            * lambda syntax as well but
                                            * note that the return
                                            * value of the lambda is
                                            * ignored. So I think this
                                            * syntax is more
                                            * helpful. */
         .walk;
}

Ali



More information about the Digitalmars-d-learn mailing list