Skip to content

Instantly share code, notes, and snippets.

@bdarnell
Created February 25, 2015 03:14
Show Gist options
  • Save bdarnell/f87608227a17df24730b to your computer and use it in GitHub Desktop.
Save bdarnell/f87608227a17df24730b to your computer and use it in GitHub Desktop.
Python yield_loop macro
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