Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Forked from bdarnell/run.py
Last active August 29, 2015 14:17
Show Gist options
  • Save alejandrobernardis/a8ec365a64f65bd7af8b to your computer and use it in GitHub Desktop.
Save alejandrobernardis/a8ec365a64f65bd7af8b to your computer and use it in GitHub Desktop.
import macropy.activate
import test
from yield_loop import macros, yield_loop
from macropy.tracing import macros, show_expanded
with show_expanded:
def f():
with yield_loop(i, some_aysnc_iterator()):
print(i)
from macropy.core.macros import *
from macropy.core.quotes import macros, q, ast, name
macros = Macros()
@macros.block
def yield_loop(tree, gen_sym, args, **kw):
# We can't use 'target' because of https://github.com/lihaoyi/macropy/issues/64.
target, args = args
iter = gen_sym()
with q as new_tree:
name[iter] = ast[args]
while (yield name[iter].fetch_next()):
ast[target] = name[iter].next_object
# Is there some form similar to ast[tree] that can be used in the above
# quasiquoted block? ast[] seems to want expressions, not statements.
new_tree[-1].body += tree
return new_tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment