1 # Coroutine example: controlling multiple instances of a single function
3 from Coroutine
import *
5 # fringe visits a nested list in inorder, and detaches for each non-list
6 # element; raises EarlyExit after the list is exhausted
9 if type(x
) is type([]):
14 def printinorder(list):
16 f
= co
.create(fringe
, co
, list)
24 printinorder([1,2,3]) # 1 2 3
25 printinorder([[[[1,[2]]],3]]) # ditto
26 x
= [0, 1, [2, [3]], [4,5], [[[6]]] ]
27 printinorder(x
) # 0 1 2 3 4 5 6
29 # fcmp lexicographically compares the fringes of two nested lists
31 co1
= Coroutine(); f1
= co1
.create(fringe
, co1
, l1
)
32 co2
= Coroutine(); f2
= co2
.create(fringe
, co2
, l2
)
49 co1
.kill(); co2
.kill()
52 print fcmp(range(7), x
) # 0; fringes are equal
53 print fcmp(range(6), x
) # -1; 1st list ends early
54 print fcmp(x
, range(6)) # 1; 2nd list ends early
55 print fcmp(range(8), x
) # 1; 2nd list ends early
56 print fcmp(x
, range(8)) # -1; 1st list ends early
57 print fcmp([1,[[2],8]],
59 print fcmp([1,[[3],8]],
61 print fcmp([1,[[2],8]],