diff --git a/documentation/coffee/slices.coffee b/documentation/coffee/slices.coffee
new file mode 100644
index 00000000..4b67581c
--- /dev/null
+++ b/documentation/coffee/slices.coffee
@@ -0,0 +1,6 @@
+numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+copy = numbers[0...numbers.length]
+
+middle = copy[3..6]
+
diff --git a/documentation/coffee/splices.coffee b/documentation/coffee/splices.coffee
new file mode 100644
index 00000000..ecf9d578
--- /dev/null
+++ b/documentation/coffee/splices.coffee
@@ -0,0 +1,5 @@
+numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+numbers[3..6] = [-3, -4, -5, -6]
+
+
diff --git a/documentation/index.html.erb b/documentation/index.html.erb
index 8247ea03..6e985acf 100644
--- a/documentation/index.html.erb
+++ b/documentation/index.html.erb
@@ -46,6 +46,7 @@
Splats...
While, Until, and Loop
Comprehensions (Arrays, Objects, and Ranges)
+ Array Slicing and Splicing
Everything is an Expression
The Existential Operator
Classes, Inheritance, and Super
@@ -529,6 +530,22 @@ coffee --bare --print --stdio
loop, for speed or for another reason, you can use
for all key, value of object in CoffeeScript.
+ + Array Slicing and Splicing with Ranges + Ranges can also be used to extract slices of arrays. + With two dots (3..6), the range is inclusive (3, 4, 5, 6); + with three docs (3...6), the range excludes the end (3, 4, 5). +
+ <%= code_for('slices', 'middle') %> ++ The same syntax can be used with assignment to replace a segment of an array + with new values, splicing it. +
+ <%= code_for('splices', 'numbers') %> ++ Note that JavaScript strings are immutable, and can't be spliced. +
Everything is an Expression (at least, as much as possible)
@@ -1019,7 +1036,7 @@ coffee --bare --print --stdio
Improved syntax errors for invalid CoffeeScript. undefined now
works like null, and cannot be assigned a new value.
There was a precedence change with respect to single-line comprehensions:
- result = i for i in list
used to parse as result = (i for i in list)
+ result = i for i in list
used to parse as result = (i for i in list)
by default ... it now parses as
(result = i) for i in list.
+ + Array Slicing and Splicing with Ranges + Ranges can also be used to extract slices of arrays. + With two dots (3..6), the range is inclusive (3, 4, 5, 6); + with three docs (3...6), the range excludes the end (3, 4, 5). +
+numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +copy = numbers[0...numbers.length] + +middle = copy[3..6] + +
var copy, middle, numbers; +numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; +copy = numbers.slice(0, numbers.length); +middle = copy.slice(3, 6 + 1); +
+ The same syntax can be used with assignment to replace a segment of an array + with new values, splicing it. +
+numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +numbers[3..6] = [-3, -4, -5, -6] + + +
var numbers, _ref; +numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; +([].splice.apply(numbers, [3, 6 - 3 + 1].concat(_ref = [-3, -4, -5, -6])), _ref); +
+ Note that JavaScript strings are immutable, and can't be spliced. +
Everything is an Expression (at least, as much as possible)
@@ -1523,10 +1563,10 @@ healthy = 200
@@ -1879,8 +1919,8 @@ task('build:parser
{range} = @variable.properties.pop()
- name = @variable.compile o
- plus = if range.exclusive then '' else ' + 1'
- from = if range.from then range.from.compile(o) else '0'
- to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
- ref = o.scope.freeVariable 'ref'
- val = @value.compile(o)
- "([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
+ name = @variable.compile o
+ plus = if range.exclusive then '' else ' + 1'
+ from = if range.from then range.from.compile(o) else '0'
+ to = "#{name}.length" unless range.to
+ unless to
+ if range.from and range.from.isSimpleNumber() and range.to.isSimpleNumber()
+ to = (+range.to.compile(o)) - +from + +plus
+ else
+ to = range.to.compile(o) + ' - ' + from + plus
+ val = @value.compile(o)
+ "[].splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"
#### Code
diff --git a/test/test_splats.coffee b/test/test_splats.coffee
index 49fc2929..c8eed486 100644
--- a/test/test_splats.coffee
+++ b/test/test_splats.coffee
@@ -123,3 +123,13 @@ try
failed = false
catch err
ok failed
+
+
+# multiple generated references
+(->
+ a = {b: []}
+ a.b[true] = -> this == a.b
+ c = 0
+ d = []
+ ok a.b[0<++c<2] d...
+)()
var cholesterol, healthy;
cholesterol = 127;
-healthy = 200 > cholesterol && cholesterol > 60;
+healthy = (200 > cholesterol && cholesterol > 60);
+healthy = (200 > cholesterol && cholesterol > 60);;alert(healthy);'>run: healthy