Presenting Fibers as Ranges

Learn about the use of “std.concurrency.Generators” for presenting fibers as ranges.

We'll cover the following

std.concurrency.Generator

Although we have achieved generating the Fibonacci series with a fiber, this implementation has the following shortcomings:

  • The solution does not provide a range interface, making it incompatible with existing range algorithms.

  • Presenting the elements by mutating a ref parameter is less desirable compared to a design where the elements are copied to the caller’s context.

  • Constructing and using the fiber explicitly through its member functions exposes lower level implementation details, compared to alternative designs.

The std.concurrency.Generator class addresses all of these issues. Note how fibonacciSeries() below is written as a simple function. The only difference is that, instead of returning a single element by return, it can make multiple elements available by yield() (infinite elements in this example).

Also, note that, this time, it is the std.concurrency.yield function, not the Fiber.yield member function that we used above.

Get hands-on with 1200+ tech skills courses.