Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Created September 18, 2013 11:08
Show Gist options
  • Save huseyinyilmaz/6607646 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/6607646 to your computer and use it in GitHub Desktop.
corecursive fibonacci (source: http://en.wikipedia.org/wiki/Corecursion)
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