implemented for-from-to and removed dotted ranges

This commit is contained in:
satyr
2010-10-13 13:53:56 +09:00
parent 2f7c076a50
commit bd10c2f828
21 changed files with 582 additions and 877 deletions

View File

@@ -177,7 +177,7 @@ ok obj.func().name is 'Dog'
class Mini
num: 10
generate: =>
for i in [1..3]
for i from 1 to 3
=>
@num

View File

@@ -15,37 +15,15 @@ ok odds.join(' ') is "one! three!"
# Basic range comprehensions.
nums = i * 3 for i in [1..3]
negs = x for x in [-20..-5*2]
negs = negs[0..2]
result = nums.concat(negs).join(', ')
ok result is '3, 6, 9, -20, -19, -18'
ok i is 3
ok x is -10
nums = i * 3 for i from 1 to 3
negs = x for x from -20 to -5*2
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
# With range comprehensions, you can loop in steps.
results = x for x in [0...15] by 5
ok results.join(' ') is '0 5 10'
results = x for x in [0..100] by 10
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
# And can loop downwards, with a negative step.
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(' ')
eq "#{ x for x from 0 to 9 by 3 }", '0,3,6,9'
eq "#{ x for x from 9 to 0 by -3 }", '9,6,3,0'
eq "#{ x for x from 3*3 to 0*0 by 0-3 }", '9,6,3,0'
# Multiline array comprehension with filter.
@@ -53,13 +31,19 @@ evens = for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
num *= -1
num -= 2
num * -1
eq evens + '', '4,6,8'
ok evens.join(', ') is '4, 6, 8'
# Backward traversing.
odds = num for num in [0, 1, 2, 3, 4, 5] by -2
eq odds + '', '5,3,1'
# The in operator still works, standalone.
ok 2 of evens
# all/from/to aren't reserved.
all = from = to = 1
# Ensure that the closure wrapper preserves local variables.
obj = {}
@@ -80,7 +64,7 @@ ok i is 3
# Ensure that local variables are closed over for range comprehensions.
funcs = for i in [1..3]
funcs = for i from 1 to 3
-> -i
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
@@ -111,19 +95,14 @@ ok methods[0]() is 'one 0'
ok methods[1]() is 'three 2'
# Naked ranges are expanded into arrays.
array = [0..10]
ok(num % 2 is 0 for num in array by 2)
# Nested comprehensions.
multiLiner =
for x in [3..5]
for y in [3..5]
for x from 3 to 5
for y from 3 to 5
[x, y]
singleLiner =
[x, y] for y in [3..5] for x in [3..5]
[x, y] for y from 3 to 5 for x from 3 to 5
ok multiLiner.length is singleLiner.length
ok 5 is multiLiner[2][2][1]
@@ -159,11 +138,6 @@ ok own.join(' ') is 'Whiskers'
ok all.sort().join(' ') is 'Whiskers cream tabby'
# Optimized range comprehensions.
exxes = 'x' for [0...10]
ok exxes.join(' ') is 'x x x x x x x x x x'
# Comprehensions safely redeclare parameters if they're not present in closest
# scope.
rule = (x) -> x
@@ -173,4 +147,4 @@ learn = ->
ok learn().join(' ') is '1 2 3'
ok rule(101) is 101
ok rule(101) is 101

View File

@@ -62,11 +62,6 @@ ok Math.AnonymousAdd(10, 10) is 20
ok Math.FastAdd(20, 20) is 40
# Parens are optional on simple function calls.
ok 100 > 1 if 1 > 0
ok true unless false
ok true for i in [1..3]
okFunc = (f) -> ok(f())
okFunc -> true
@@ -122,7 +117,7 @@ mult = (x, mids..., y) ->
ok mult(1, 2,) is 2
ok mult(1, 2, 3,) is 6
ok mult(10,[1..6]...,) is 7200
ok mult(10, (i for i from 1 to 6)...) is 7200
# Test for inline functions with parentheses and implicit calls.

View File

@@ -1,6 +1,6 @@
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
array = [0..4]
array = [0, 1, 2, 3, 4]
string = array.join ''
object = {}

View File

@@ -14,10 +14,6 @@ ok value is 1
value = 0.0 + -.25 - -.75 + 0.0
ok value is 0.5
# Decimals don't interfere with ranges.
ok [0..10].join(' ') is '0 1 2 3 4 5 6 7 8 9 10'
ok [0...10].join(' ') is '0 1 2 3 4 5 6 7 8 9'
# Can call methods directly on numbers.
4.valueOf() is 4

View File

@@ -1,75 +0,0 @@
# Slice.
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = array[7..9]
b = array[2...4]
result = a.concat(b).join(' ')
ok result is "7 8 9 2 3"
a = [0, 1, 2, 3, 4, 5, 6, 7]
eq a[2...6].join(' '), '2 3 4 5'
# Ranges.
countdown = [10..1].join(' ')
ok countdown is "10 9 8 7 6 5 4 3 2 1"
a = 1
b = 5
nums = [a...b]
ok nums.join(' ') is '1 2 3 4'
b = -5
nums = [a..b]
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
# Expression-based range.
array = [(1+5)..1+9]
ok array.join(' ') is "6 7 8 9 10"
array = [5..1]
ok array.join(' ') is '5 4 3 2 1'
array = [30...0]
ok (len = array.length) is 30
ok array[len - 1] is 1
# String slicing (at least on Node).
hello = "Hello World"
ok hello[1...1] is ""
ok hello[1..1] is "e"
ok hello[1...5] is "ello"
ok hello[0..4] is "Hello"
# Splice literals.
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array[5..10] = [0, 0, 0]
ok array.join(' ') is '0 1 2 3 4 0 0 0'
# Slices and splices that omit their beginning or end.
array = [0..10]
ok array[7..].join(' ') is '7 8 9 10'
ok array[-2..].join(' ') is '9 10'
ok array[...3].join(' ') is '0 1 2'
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
array[3..] = [9, 8, 7]
ok array.join(' ') is '0 1 2 9 8 7'
array[...3] = [7, 8, 9]
ok array.join(' ') is '7 8 9 9 8 7'

View File

@@ -37,7 +37,7 @@ ok last is "Usain Bolt"
ok theField.length is 8
contenders.reverse()
medalists contenders[0...2]..., "Mighty Mouse", contenders[2...contenders.length]...
medalists contenders.slice(0, 2)..., "Mighty Mouse", contenders.slice(2)...
ok gold is "Usain Bolt"
ok silver is "Asafa Powell"
@@ -69,7 +69,7 @@ crowd = [
bests = [
"Mighty Mouse"
contenders[0..3]...
contenders.slice(0, 4)...
]
ok crowd[0] is contenders[0]