Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an additional code example under the Generators section #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/iterators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------
Expand Down