@@ -144,22 +144,26 @@ def walker(head):
144144class BinaryTreeIterator (ConsIterator ):
145145 """Iterator for binary trees built from cons cells."""
146146 def __init__ (self , root ):
147- # def walker(cell): # FP, call stack overflow for deep trees
148- # for x in (cell.car, cell.cdr):
149- # if isinstance(x, cons):
150- # yield from walker(x)
151- # else:
152- # yield x
147+ # def walker(cell): # FP, call stack overflow for deep trees
148+ # for x in (cell.car, cell.cdr):
149+ # if isinstance(x, cons):
150+ # yield from walker(x)
151+ # else:
152+ # yield x
153153 def walker (cell ): # imperative, no call stack overflow (we keep our own data stack instead)
154154 stack = [cell ]
155155 while stack :
156156 cell = stack .pop ()
157157 a , d = cell .car , cell .cdr
158158 ac , dc = isinstance (a , cons ), isinstance (d , cons )
159- if not ac : yield a
160- if not dc : yield d
161- if dc : stack .append (d )
162- if ac : stack .append (a ) # LIFO
159+ if not ac :
160+ yield a
161+ if not dc :
162+ yield d
163+ if dc :
164+ stack .append (d )
165+ if ac :
166+ stack .append (a ) # LIFO
163167 super ().__init__ (root , walker )
164168
165169class JackOfAllTradesIterator (ConsIterator ):
@@ -180,26 +184,30 @@ class JackOfAllTradesIterator(ConsIterator):
180184 the specific kind of cons structure you have.
181185 """
182186 def __init__ (self , root ):
183- # @gtrampolined
184- # def walker(cell): # FP, tail-recursive in the cdr half only
185- # if isinstance(cell.car, cons):
186- # yield from walker(cell.car)
187- # else:
188- # yield cell.car
189- # if isinstance(cell.cdr, cons):
190- # return walker(cell.cdr) # signal gtrampolined to tail-chain
191- # elif cell.cdr is not nil:
192- # yield cell.cdr
187+ # @gtrampolined
188+ # def walker(cell): # FP, tail-recursive in the cdr half only
189+ # if isinstance(cell.car, cons):
190+ # yield from walker(cell.car)
191+ # else:
192+ # yield cell.car
193+ # if isinstance(cell.cdr, cons):
194+ # return walker(cell.cdr) # signal gtrampolined to tail-chain
195+ # elif cell.cdr is not nil:
196+ # yield cell.cdr
193197 def walker (cell ):
194198 stack = [cell ]
195199 while stack :
196200 cell = stack .pop ()
197201 a , d = cell .car , cell .cdr
198202 ac , dc = isinstance (a , cons ), isinstance (d , cons )
199- if not ac : yield a
200- if not dc and d is not nil : yield d
201- if dc : stack .append (d )
202- if ac : stack .append (a ) # LIFO
203+ if not ac :
204+ yield a
205+ if not dc and d is not nil :
206+ yield d
207+ if dc :
208+ stack .append (d )
209+ if ac :
210+ stack .append (a ) # LIFO
203211 super ().__init__ (root , walker )
204212
205213class cons :
0 commit comments