defproc_seq(n,seq,alive,update,init,debug=False):"""Process a sequence of items.
n: seed
seq: function that produces the next item in the sequence
alive: function that tests whether to continue processing
update: function that updates the state
init: initial state
"""store=initwhilealive(n):store=update(store,n)n=seq(n)ifdebug:print(store,n)returnstoredefproc(state,alive,update,debug=False,display=lambdax:print(x)orx):"""Process as succession of states.
state: initial state
alive: one-place function that tests whether state is alive
update: function that updates the state
"""whilealive(state):state=update(state)ifnotdebugelsedisplay(update(state))returnstatedefmake_generator(seq,init):"""Create a generator from a sequence function.
seq: function that produces the next item in the sequence
init: initial state
"""deff():nonlocalinitinit=seq((result:=init))returnresultreturnf