Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Create a struct with, minimally, the three core input range members: the properties front, the properties of empty, and the function popFront. It should also hold whatever state is necessary for iteration with successive calls to popFront.

  2. 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.

  3. Test it with static assert for the interface you need and perform unit tests.

  4. Write a helper method...