Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Created August 14, 2013 00:54
Show Gist options
  • Save weskerfoot/6227122 to your computer and use it in GitHub Desktop.
Save weskerfoot/6227122 to your computer and use it in GitHub Desktop.
from itertools import *
class Cont(object):
def __init__(self, func, *args):
self.args = args
self.func = func
self.done = False
def next(self):
if self.done:
raise StopIteration
result = self.func(*self.args)
if isinstance(result, Cont):
self.func = result.func
self.args = result.args
return result
else:
self.done = True
return result
def __iter__(self):
return self
def gcd(x,y, msg):
print msg
if y == 0:
return x
return Cont(gcd, y, x % y, msg)
def factorial(acc, n):
if n == 0:
return acc
else:
return Cont(factorial, acc*n, n-1)
foo = gcd(1345, 2332434, "lol")
bar = gcd(1345, 2223150, "hey")
print list(((x,y) for x,y in izip(foo, bar)))
foo = gcd(1345, 2332434, "lol")
bar = gcd(1345, 2223150, "hey")
print list(x for x in bar for y in foo)
bar = gcd(1345, 2223150, "hey")
foo = gcd(1345, 2332434, "lol")
print list(islice(foo, 0, 3))
print list(factorial(1,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment