In coffescript I have
arr = ["a","b","c"]
for i in [0..arr.length] by 1
if (sometimesTrue)
arr.pop()
i--
But it is translating it to this:
var arr, i, _i, _ref;
arr = ["a", "b", "c"];
for (i = _i = 0, _ref = arr.length; _i <= _ref; i = _i += 1) {
if (sometimesTrue) {
arr.pop();
i--;
}
}
You can see that this loop uses a _i
as the reference rather than i
so my i--
doesn't really do anything.
Since in this loop, the length of the array changes, I sort of need to figure out how to handle this... Is there any way to do this with a for loop? Or do I need to switch to a while?
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:
becomes this:
Note that the number of iterations is fixed when
_ref
is computed at the beginning of the loop. Also note thati
will be assigned a new value on each iteration so any changes you make toi
inside the loop will be ignored. And, note that looping over[0..a.length]
doesa.length+1
iterations, nota.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:becomes this:
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:or:
Using indices in coffeescript is quite unnatural.
I think that what you want is something like :
I think that you should read the following topic "Loops and Comprehensions" at http://coffeescript.org/
Modifying the array you're looping over rarely does what you want in languages with
for ... in ...
constructs. What you're really looking for is afilter
. Many javascript implementations have a filter function attached to the array prototype:If you can't count on this, you can use a similar CoffeeScript construct:
Sometimes it helps to take a walk, maybe in real fresh air in the woods, maybe in front of the house with a cigarrette (though your wife might hate the smoking) ... Somtimes it even helps to talk to the rubber duck some uber-geeks have on their monitors. And sometimes it plain and simple helps to attack the simple things from a different angle.
arr = ["a", "b", "c"]EDIT: As seen in the comments below I was missing something here. I is indeed not ignored at all, how could I've thought that in the first place?