Created
October 20, 2012 15:49
-
-
Save fogleman/3923674 to your computer and use it in GitHub Desktop.
Simpler Fibonacci Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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