You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
for i in range(a, b, c) is not translated to for (;;) loop #625
If range has extra arguments, it treats for loop like a loop over regular sequence.
It should be fairly trivial to make for i in range(begin, end, step)
to translate into for (i=begin; i < end; i += step)
After looking at the js source, my guess would be that the transpiler cannot know at compile time whether or not var step(edited) is positive or negative, so it can't decide which of i < end; and i > end; is the right code. This is taken care of at runtime by the range function.
maybe using != instead of < or > would solve the issue when step is not given or constant 1 or -1? using range() is a major overhead since it generates array every time. Don't want to resort to using while as it hurts readability.
Another gotcha in the for loop:
in python the loop for i in range(10): assigns value to i each iteration so in the iteration you can modify it without fear. In transcrypt if it is unrolled into for (i = 0; i < 10; ++i) this will break the code if you will try to modify i within iteration.
Suggested solution - use temporary variable for looping and assign it to i each iteration
Activity
AlexECX commentedon Apr 25, 2019
Just tried it and it's working,
for i in range(0, 5, 2)givesfor (var i = 0; i < 5; i += 2).faerot commentedon May 11, 2019
Maybe it is working for constants, try using variables as arguments to see the bug.
AlexECX commentedon May 12, 2019
After looking at the js source, my guess would be that the transpiler cannot know at compile time whether or not
var step(edited) is positive or negative, so it can't decide which ofi < end;andi > end;is the right code. This is taken care of at runtime by therangefunction.faerot commentedon May 12, 2019
maybe using != instead of < or > would solve the issue when step is not given or constant 1 or -1? using range() is a major overhead since it generates array every time. Don't want to resort to using while as it hurts readability.
AlexECX commentedon May 12, 2019
Based on the
range()function implementation, perhaps this could do:It's about <1.5x slower than directly writing it in javascript, but orders of magnitude faster than the current implementation.
faerot commentedon May 14, 2019
indeed, looks like a great solution for what it's worth
faerot commentedon Jun 10, 2019
Another gotcha in the for loop:
in python the loop for i in range(10): assigns value to i each iteration so in the iteration you can modify it without fear. In transcrypt if it is unrolled into for (i = 0; i < 10; ++i) this will break the code if you will try to modify i within iteration.
Suggested solution - use temporary variable for looping and assign it to i each iteration
AlexECX commentedon Jun 18, 2019
Already gives