[Issue 5660] yield syntax sugar

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 27 18:11:22 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5660


David Simcha <dsimcha at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha at yahoo.com


--- Comment #1 from David Simcha <dsimcha at yahoo.com> 2011-02-27 18:08:32 PST ---
opApply plus fibers should do what you need.  It's inefficient, but so is
pretty much any coroutine/yield-based way of doing things, including Python's. 
I was thinking a while back that something like this belonged in std.range, but 
I wanted to handle ref properly, so Bug 2443 got in my way.

Example (Warning:  Untested.):

/**
This must be a class or otherwise have reference/heap-allocated semantics.
*/
class OpApplyToRange(Iterable) {
    Fiber fiber;
    ForeachType!Iterable _front;
    bool _empty;
    Iterable iterable;

    void doLoop() {
        foreach(elem; iterable) {
            _front = elem;
            Fiber.yield();
        }

        _empty = true;
    }

    this(Iterable iterable) {
        this.iterable = iterable;
        fiber = new Fiber(&doLoop);
        fiber.call();
    }

    void popFront() {
        fiber.call();
    }

    ForeachType!Iterable front() @property { return _front; }

    bool empty() @property { return _empty; }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list