mwchase icon

Yet more Python code that I'm not sure about

mwchase | PRO | 04/19/19 03:40:39 AM UTC | 0 ⭐ | 839 👁️ | Never ⏰ | []
Python |

680 B

|

None

|

0 👍

/

0 👎

class Action:
    def handle(self, to_process):
        raise NotImplementedError
 
 
class Yield(Action):
    def __init__(self, item):
        self.item = item
 
    def handle(self, _to_process):
        yield self.item
 
 
class Extend(Action):
    def __init__(self, iterable):
        self.iterable = iterable
 
    def handle(self, to_process):
        to_process.extend(self.iterable)
        yield from ()
 
 
def handle(action, to_process):
    if action is not None:
        yield from action.handle(to_process)
 
 
def stack_iter(first, process):
    to_process = [first]
    while to_process:
        yield from handle(process(to_process.pop()), to_process)

Comments