Created
February 25, 2015 03:14
-
-
Save bdarnell/f87608227a17df24730b to your computer and use it in GitHub Desktop.
Python yield_loop macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import macropy.activate | |
import test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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