Suspending Fibonacci

The main topic of this chapter is data sources, so we will use a well-known sequence to get an implementation of suspending sequences and iterators: the famous Fibonacci sequence. I am sure that you have already written algorithms to calculate the Fibonacci sequence before, but this may be the first time that you will do so using suspending functions.

If you aren't familiar with it, the Fibonacci sequence is a sequence of numbers in which each number is the result of adding the previous two.

You can see the first eight numbers of the sequence in the following image:

The objective that we have, then, is to write a function that returns numbers from that sequence on demand, and stays suspended between calls. We will first do an implementation with a sequence and then do a similar implementation with an iterator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197