Skip to content

Instantly share code, notes, and snippets.

@manudatta
Created August 14, 2014 04:56
Show Gist options
  • Save manudatta/16d81405464cd0e0c77a to your computer and use it in GitHub Desktop.
Save manudatta/16d81405464cd0e0c77a to your computer and use it in GitHub Desktop.
Python streams implementation sample
def ones():
return 1,lambda : ones()
def power_of_two(x=1):
return x,lambda : power_of_two(x*2)
def fibonacci(xcurr=1,xlast=0):
xnext = xcurr+xlast
return xnext,lambda : fibonacci(xnext,xcurr)
def list_iter(l=[]):
if len(l) == 0 :
raise StopIteration
return l[0],lambda :list_iter(l[1:])
if __name__ == '__main__':
i = 0
s = ones
print " ONES "
while i < 11:
x,s = s()
print x
i = i + 1
i = 0
print " POWER OF TWO "
s = power_of_two
while i < 10:
x,s = s()
print x
i = i + 1
print " FIBONNACI "
s = fibonacci
while i < 101:
x,s = s()
print x
i = i + 1
l = [1,2,3]
i = 0
x,s = list_iter(l)
while True:
try:
print x
x,s = s()
except StopIteration:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment