What is a "yield return" equivalent in the D programming language?
Asked Answered
M

3

26

Here is a simple generator in C#.

    IEnumerable<int> Foo()
    {
        int a = 1, b = 1;
        while(true)
        {
            yield return b;
            int temp = a + b;
            a = b;
            b = temp;
        }
    }

How do I write a similar generator in Digital Mars D?

(The question is about the yield return statement)

Thanks!


Update. That's interesting. Since I'm just generating a mathematical sequence, using recurrence may be a good option.

auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);

foreach (e; take(fib, 10)) // <- prints first ten numbers from the sequence
{ 
    writeln(e); 
}
Michelemichelina answered 4/10, 2010 at 15:26 Comment(2)
Note that you should be able to write fib.take(10) by now cause Uniform Function Call Syntax has been enhanced.Delija
Even found the article: drdobbs.com/blogs/cpp/232700394Delija
R
20

There's no exact equivalent in D. Here are some rough equivalents:

Using opApply-style internal iteration. This doesn't allow iterating over two iterators in lockstep, though:

struct Foo {
    int opApply(int delegate(ref int) dg) {
        int a = 1, b = 1;
        int result;
        while(true) {
            result = dg(b);
            if(result) break;
            int temp = a + b;
            a = b;
            b = temp;
        }

        return result;
    }
}

void main() {
    // Show usage:
    Foo foo;
    foreach(elem; foo) {
        // Do stuff.
    }
}

Use ranges. These are slightly harder to write in some cases, but are very efficient and allow lockstep iteration. This can also be iterated over with a foreach loop, exactly like the opApply version:

struct Foo {
    int a = 1, b = 1;

    int front() @property {
        return b;
    }

    void popFront() {
        int temp = a + b;
        a = b;
        b = temp;
    }

    // This range is infinite, i.e. never empty.
    enum bool empty = false;

    typeof(this) save() @property { return this; }
}

If you really need coroutine-style stuff you can combine ranges and opApply together using core.thread.Fiber, but you'll probably find that either ranges or opApply does what you need almost all the time.

Ranger answered 4/10, 2010 at 16:54 Comment(3)
While the D version is a little more verbose, it is at least more readable: I just can't get myself over that Foo should return 1 every time (b is assigned 1 in the very beginning). Also every language does things a little differently, for example Ruby is basically the opApply version without the struct overhead.Menology
@he_the_great: The other tradeoff is that both D versions are much more efficient. While coroutine/generator style iteration can be simulated using fibers, I think pretty syntax for it was intentionally omitted because it costs about a hundred cycle context switch on each iteration. The opApply version costs maybe 5 or 10 cycles for the delegate call, and the range version can cost nothing compared to a hand-coded version because all of the range primitives are eligible for inlining. One of these two styles will do what you need w/o much trouble about 99.9% of the time.Ranger
Thanks, dsimcha. Using ranges looks very promising, I shall look it up.Michelemichelina
T
20

See here; example excerpt below:

module main;

import std.stdio;
import generators;

void genSquares(out int result, int from, int to)
{
    foreach (x; from .. to + 1)
        yield!result(x * x);
}

void main(string[] argv)
{
    foreach (sqr; generator(&genSquares, 10, 20))
        writeln(sqr);
}
Tao answered 16/7, 2011 at 6:35 Comment(0)
L
17

The std.concurrency module now has a Generator class which makes this even easier (and you don't need a third-party library).

The class is an input range, so it can be used with for loops and all the standard std.range/std.algorithm functions.

import std.stdio;
import std.range;
import std.algorithm;
import std.concurrency : Generator, yield;

void main(string[] args) {
    auto gen = new Generator!int({
        foreach(i; 1..10)
            yield(i);
    });

    gen
        .map!(x => x*2)
        .each!writeln
    ;
}
Legere answered 24/6, 2015 at 14:15 Comment(1)
I am quite sure the Generator template did not exist when question was asked...Helen

© 2022 - 2024 — McMap. All rights reserved.