def test_snippet_1(self): from __pypy__ import thunk def f(): print 'computing...' return 6*7 x = thunk(f)
def test_is_thunk2(self): from __pypy__ import thunk, become, is_thunk def f(): return 42 x = thunk(f) assert is_thunk(x) assert x == 42 assert not is_thunk(x)
def test_is_thunk_become(self): from __pypy__ import thunk, become, is_thunk def f(): return 42 x = thunk(f) y = [] become(y, x) assert is_thunk(y) assert y == 42 assert not is_thunk(y)
def send(self, obj, n=None): #print 'send', n,; self._check(obj) if n is None: n = self.count self.count += 2 data = (n, obj, None) else: data = (n, obj) self.sendraw(data) become(obj, thunk(self._resume, n))
def test_method_call(self): from __pypy__ import thunk d = {} # need the method to use the pypy compiler exec """if 1: def f(x): return [x] def g(l): l.append(1) """ in d l = thunk(d['f'], 10) d['g'](l) assert l == [10, 1]
def test_simple(self): from __pypy__ import thunk, become computed = [] def f(): computed.append(True) return 6*7 x = thunk(f) assert computed == [] t = type(x) assert t is int assert computed == [True] t = type(x) assert t is int assert computed == [True]
def test_setitem(self): from __pypy__ import thunk, become computed = [] def f(a): computed.append(True) return a*7 x = thunk(f, 6) d = {5: x} d[6] = x d[7] = [] d[7].append(x) assert computed == [] y = d[5], d[6], d.values(), d.items() assert computed == [] d[7][0] += 1 assert computed == [True] assert d[7] == [43]
def test_is_thunk(self): from __pypy__ import thunk, become, is_thunk def f(): pass assert is_thunk(thunk(f)) assert not is_thunk(42)
def test_thunk_forcing_while_forcing_2(self): from __pypy__ import thunk, become def f(): return x x = thunk(f) raises(RuntimeError, 'x+1')
def test_thunk_special_method(self): skip("fix me") from __pypy__ import thunk x = thunk(lambda : 42) assert 1 .__add__(x) == 43
def add_lists(list1, list2): """Compute the linked-list equivalent of the Python expression [a+b for (a,b) in zip(list1,list2)] """ return ListNode(list1.head + list2.head, thunk(add_lists, list1.tail, list2.tail))
print __doc__ raise SystemExit(2) # ____________________________________________________________ class ListNode: def __init__(self, head, tail): self.head = head # the first element of the list self.tail = tail # the sublist of all remaining elements def add_lists(list1, list2): """Compute the linked-list equivalent of the Python expression [a+b for (a,b) in zip(list1,list2)] """ return ListNode(list1.head + list2.head, thunk(add_lists, list1.tail, list2.tail)) # 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Fibonacci = ListNode(1, ListNode(1, None)) Fibonacci.tail.tail = thunk(add_lists, Fibonacci, Fibonacci.tail) if __name__ == '__main__': node = Fibonacci while True: print node.head node = node.tail