Created
September 18, 2013 11:08
-
-
Save huseyinyilmaz/6607646 to your computer and use it in GitHub Desktop.
corecursive fibonacci (source: http://en.wikipedia.org/wiki/Corecursion)
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
from itertools import tee, chain, islice, imap | |
def add(x, y): | |
return x + y | |
def fibonacci(): | |
def deferred_output(): | |
for i in output: | |
yield i | |
result, c1, c2 = tee(deferred_output(), 3) | |
paired = imap(add, c1, islice(c2, 1, None)) | |
output = chain([0, 1], paired) | |
return result | |
for i in islice(fibonacci(), 20): | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment