mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
enhancements to range comprehensions, back to being safe -- and usable downwards without a 'by' clause, and optimized when working with integer literals.
This commit is contained in:
@@ -25,14 +25,6 @@ result: nums.concat(negs).join(', ')
|
||||
ok result is '3, 6, 9, -20, -19, -18'
|
||||
|
||||
|
||||
# Ensure that ranges are safe. This used to infinite loop:
|
||||
j = 5
|
||||
result: for j in [j..(j+3)]
|
||||
j
|
||||
|
||||
ok result.join(' ') is '5 6 7 8'
|
||||
|
||||
|
||||
# With range comprehensions, you can loop in steps.
|
||||
results: x for x in [0..25] by 5
|
||||
|
||||
@@ -40,11 +32,17 @@ ok results.join(' ') is '0 5 10 15 20 25'
|
||||
|
||||
|
||||
# And can loop downwards, with a negative step.
|
||||
results: x for x in [5..1] by -1
|
||||
results: x for x in [5..1]
|
||||
|
||||
ok results.join(' ') is '5 4 3 2 1'
|
||||
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
|
||||
|
||||
results: x for x in [10..1]
|
||||
ok results.join(' ') is [10..1].join(' ')
|
||||
|
||||
results: x for x in [10...0] by -2
|
||||
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
|
||||
|
||||
|
||||
# Multiline array comprehension with filter.
|
||||
evens: for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
||||
|
||||
Reference in New Issue
Block a user