diff --git a/source/iterators.rst b/source/iterators.rst index b9838f6..170c9ac 100644 --- a/source/iterators.rst +++ b/source/iterators.rst @@ -280,6 +280,19 @@ Lets see an example:: print take(5, squares()) # prints [1, 4, 9, 16, 25] +Another interesting example involves the generation of fibonacci numbers. +Using recursion to generate a large fibonacci number will take a very long time and requires additional space. +However, an implementation using a generator yields quick results. Consider the following:: + + def fibonacci_generator(): + a,b = 0,1 # Or 1,1 + while True: + yield a + a,b = b,a+b + + fib = fibonacci_generator() + + fib_nums = [fib.next() for i in xrange(100)] Generator Expressions ---------------------