Skip to content

Instantly share code, notes, and snippets.

@fogleman
Created October 20, 2012 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogleman/3923674 to your computer and use it in GitHub Desktop.
Save fogleman/3923674 to your computer and use it in GitHub Desktop.
Simpler Fibonacci Iterator
class FibonacciIterator(object):
def __init__(self):
self.data = [0, 1]
def next(self):
self.data.append(sum(self.data))
return self.data.pop(0)
class Fibonacci(object):
def __iter__(self):
return FibonacciIterator()
if __name__ == '__main__':
import itertools
for value in itertools.takewhile(lambda x: x < 50, Fibonacci()):
print value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment