What is a “yield return” equivalent in the D programming language?

前端 未结 3 678
小鲜肉
小鲜肉 2021-02-01 17:11

Here is a simple generator in C#.

    IEnumerable Foo()
    {
        int a = 1, b = 1;
        while(true)
        {
            yield return b;
             


        
3条回答
  •  面向向阳花
    2021-02-01 17:33

    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);
    }
    

提交回复
热议问题