Skip to content

Instantly share code, notes, and snippets.

@pollingj
Created July 20, 2011 16:47
Show Gist options
  • Save pollingj/1095334 to your computer and use it in GitHub Desktop.
Save pollingj/1095334 to your computer and use it in GitHub Desktop.
Odd for loop
for num in [1..starValue]
for (num = 1; 1 <= starValue ? num <= starValue : num >= starValue; 1 <= starValue ? num++ : num--)
@pollingj
Copy link
Author

I must be doing something wrong to produce this loop.

@TrevorBurnham
Copy link

Add by 1 to get the more optimal for (num = 1; num <= starValue; num += 1) on the current CoffeeScript master. (This optimization isn't yet in a tagged release, but should be in 1.1.2.)

It's a little weird, but CoffeeScript supports backward loops (e.g. for num in [1..0]); hence the verbose output if you don't explicitly specify a step.

@pollingj
Copy link
Author

Thanks @TrevorBurnham. I now have for num in [1..starValue] by 1 which now gives me for (num = 1; 1 <= starValue ? num <= starValue : num >= starValue; num += 1). as opposed to the for (num = 1; num <= starValue; num += 1) you mentioned.

@TrevorBurnham
Copy link

@pollingj As I said, the optimization is on the current master branch of the CoffeeScript compiler, but isn't in the latest tagged release (1.1.1). It will be in 1.1.2.

@pollingj
Copy link
Author

@TrevorBurnham ah, sorry, I thought you meant the requirement to not have by 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment