CoffeeScript will compute the loop bounds once and you can't reset the calculation so changing the array while you're iterating over it will just make a big mess.
For example, this:
f(i) for i in [0..a.length]
becomes this:
var i, _i, _ref;
for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
f(i);
}
Note that the number of iterations is fixed when _ref
is computed at the beginning of the loop. Also note that i
will be assigned a new value on each iteration so any changes you make to i
inside the loop will be ignored. And, note that looping over [0..a.length]
does a.length+1
iterations, not a.length
iterations; [a..b]
produces a closed interval (i.e. contains both end points), [a...b]
gives you a half-open interval (i.e. b
is not included). Similarly, this:
f(i) for i in a
becomes this:
var i, _i, _len;
for (_i = 0, _len = a.length; _i < _len; _i++) {
i = a[_i];
f(i);
}
Again, the number of iterations is fixed and changes to i
are overwritten.
If you want to mess around the the array and the loop index inside the loop then you have to do it all by hand using a while
loop:
i = 0
while i < arr.length
if(sometimesTrue)
arr.pop()
--i
++i
or:
i = 0
while i < arr.length
if(sometimesTrue)
arr.pop()
else
++i