Skip to content

Instantly share code, notes, and snippets.

@sir-wabbit
Created August 13, 2026 21:14
Show Gist options
  • Select an option

  • Save sir-wabbit/7255d3d9c22733de3dde9fd1a05a6fdd to your computer and use it in GitHub Desktop.

Select an option

Save sir-wabbit/7255d3d9c22733de3dde9fd1a05a6fdd to your computer and use it in GitHub Desktop.
from dataclasses import dataclass
@dataclass
class Stop:
value: any
@dataclass
class Resume:
value: any
def runWith(generator, handlers: list[callable]):
yield_result = None
while True:
try:
yielded = generator.send(yield_result)
for h in handlers:
r = h(yielded)
if r is None:
continue
if isinstance(r, Resume):
yielded = r
elif isinstance(r, Stop):
return r
else:
yielded = None
except StopIteration as s:
return s.value
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def genFibs(limit: int) -> list[int]:
count = 0
fibs = []
def handler(i):
nonlocal count, fibs
if not isinstance(i, int):
return None
if count < limit:
count += 1
fibs.append(i)
return Resume(None)
return Stop(None)
runWith(fib(), [handler])
return fibs
print(genFibs(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment