Fibers and Ranges

Freddy via Digitalmars-d digitalmars-d at puremagic.com
Sun Oct 4 16:40:22 PDT 2015


---
import core.thread;
import std.conv;
import std.stdio;

void formatStuff(P)(P put, int stuff)
{
     put("a");
     put("b");
     put("c");
     put(stuff);
     foreach (i; 0 .. 10)
     {
         put(i ^^ stuff);
     }
}

auto formatRange(alias sub, T...)(T args)
{
     class FormatFiber : Fiber
     {
         string front_;
         this()
         {
             super(&run);
             popFront;
         }

         void run()
         {
             sub(this, args);
         }

         void opCall(T)(T t)
         {
             front_ = t.to!string;
             yield;
         }

         void popFront()
         {
             call;
         }

     @property:
         string front()
         {
             return front_;
         }

         bool empty()
         {
             return state != State.HOLD;
         }
     }

     return new FormatFiber;
}

void main()
{
     writeln(formatRange!formatStuff(5));
}
---
Another Idea thoughts?


More information about the Digitalmars-d mailing list