1

It seems like a simple conversion, but I can't seem to find the syntax for it.

i = start
while(if step > 0 then i < end else i > end)
  array.push i
  i += step

start, end, and step are a signed integers

2
  • start, end, and step are a signed integers Commented Jun 1, 2012 at 14:39
  • 1
    Oh, "signed integers", right. But that wan't my question. If step is 1 and start is 0 and end is 0 you'll end up in an infinite loop. So it does matter how those are initialized before the loop, and this may be important for converting the code. Commented Jun 1, 2012 at 14:50

2 Answers 2

2

This may do what you want, assuming that you want the numbers from start to end as items in the array variable:

array = (i for i in [start...end])
Sign up to request clarification or add additional context in comments.

Comments

1

You should read the CofeeScript page on loops. But the way to do this in CoffeeScript is with a list comprehension iterating over a range:

(array.push i for i in [start...end] by step)

But note that a list comprehension returns a value. For instance, given this code:

start = 10
end = 5
step = -2
array = []
(array.push i for i in [start...end] by step)

The variable array winds up with the value [10,8,6], as expected, but since push returns the new length of the array it just pushed onto, the return value of that last statement - which will be returned if it's the last thing in a function, or printed if you enter the above at the REPL, etc. - is [1, 2, 3].

EDIT So it would be better, as noted below, to just construct the array with the list comprehension in the first place:

array = (i for i in [start...end] by step)

When building a range, note that ... yields an exclusive range in terms of the right endpoint, while .. yields an inclusive one. So [1..5] includes 5 in the list, while [1...5] stops at 4.

Also, if you really find yourself needing the flexibility of the C-style for loop, you can always embed some literal JavaScript in your CoffeeScript by wrapping it in backticks (`...`):

`for (i=start; (step > 0 && i < end) || (step < 0 && i > end); i+=step) {
     array.push(i);
 }`

5 Comments

that is the equivalent of while(if step > 0 then i <= end else i >= end) but, I need while(if step > 0 then i < end else i > end)
@user842893 - change the dots in the range. [a..b] is inclusive, [a...b] is exclusive. I'll edit.
I just would like the fine control of the while loop, in the cleaner syntax of the for loop. I don't know if coffee script allows that.
@user842893 - you can always break into native JavaScript for the extra flexibility if you really need it somewhere. Added an example of that, too.
@MarkReed Your solution will work, but as you said, the loop returns a value. You take out the array.push and use the return value of the loop directly. array = (i for i in [start...end] by step).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.