On Saturday, 30 May 2015 at 23:58:44 UTC, Ali Çehreli wrote:
On 05/30/2015 12:19 PM, Dennis Ritchie wrote:

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

Nothing to worry about! Now you will understand me till the end... :)

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:

Yes, it is an intermediate stage of code that will help me to explain what I want to get.

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); // It's a necessary part of the code! :)
        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

I don't know, maybe it's something out of science fiction, but here's what I want to do :)

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

    alias range this;

    @property auto front()
    {
        immutable string myStr = `func(mixin("range.front"));`;
        immutable string newStr = `mixin(myStr);`;
writeln(newStr); // I want them to be printed here `writeln([5]);` mixin(newStr[0 .. $ - 4] ~ `[idx,` ~ newStr[$ - 4 .. $]); // I want them to be printed here `[0, 5];`
        return range.front;
    }
}

Is it possible to do using mixins or through any other means? Ie I want to catch the moment when `func` is replaced by the invocation of `writeln` :)

Reply via email to