Creating an input range
Input ranges are D's improvement to the iterators of C++ that are safer, potentially more efficient, and can also generate their own data. Here, we'll create an input range that generates numbers in a Fibonacci sequence to see how it can be done and briefly explore how well it integrates with std.algorithm
automatically.
How to do it…
Let's execute the following steps to create an input range that generates numbers in a Fibonacci sequence:
Create a struct with, minimally, the three core input range members: the properties
front
, the properties ofempty
, and the functionpopFront
. It should also hold whatever state is necessary for iteration with successive calls topopFront
.Add all other functionality as possible without breaking the complexity guarantee. Our Fibonacci generator can also implement a simple
save
property, so we will add it, upgrading it to a forward range.Test it with
static assert
for the interface you need and perform unit tests.Write a helper method...