Skip to content

Commit 7ab19c2

Browse files
committed
Lazy consumption of generators zipped with other iterables
1 parent 793e18e commit 7ab19c2

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

src/transpiler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1190,11 +1190,11 @@ def transpileStmtFor(self, node):
11901190
update = v.Call('safe_advance', iterator, end, abs_step)
11911191

11921192
conditions.append(cond)
1193-
updates.append(update)
1193+
updates.append(f'({update}, {cond})')
11941194
getters.append(getter)
11951195

11961196
condition = ' && '.join(str(cond) for cond in conditions)
1197-
update = ', '.join(str(update) for update in updates)
1197+
update = ' && '.join(str(update) for update in updates)
11981198

11991199
with self.loop(partial(c.For, '', condition, update), node.get('label')):
12001200
if len(vars) == 1 and len(types) > 1:

test/good/generators/generator11.out

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2
2+
1
3+
4
4+
2 1 4
5+
3
6+
null 1 1

test/good/generators/generator11.px

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
func range(a, b): Int... def
3+
for x in a..b do
4+
print x
5+
yield x
6+
print null, a, b
7+
8+
for x, y, z in range(2, 3), range(1, 1), range(4, 5) do
9+
print x, y, z

test/good/generators/var03.out

+8
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ true
77
4
88
5
99
[5]
10+
true
11+
1
12+
2
13+
[1, 2, 3]
14+
3
15+
4
16+
5
17+
[4, 5]

test/good/generators/var03.px

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ print false
1111
print [for v, i in r, 0...3 yield v]
1212
# 4 is lost here, Python behaves the same way.
1313
print [...r]
14+
15+
r = counter(5)
16+
17+
print [for i, v in 0...3, r yield v]
18+
# But with another order of iterables, 4 is consumed properly.
19+
print [...r]

version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.12.0.dev
1+
0.13.0

0 commit comments

Comments
 (0)