From 75ca0f23ac2225b23e54a5c1f2a72715573b80df Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 4 Dec 2010 12:52:51 -0500 Subject: [PATCH 1/5] redocumenting slices/splices ... issue #833 --- documentation/coffee/slices.coffee | 6 ++++ documentation/coffee/splices.coffee | 5 +++ documentation/index.html.erb | 21 ++++++++++-- documentation/js/comparisons.js | 2 +- documentation/js/slices.js | 4 +++ documentation/js/splices.js | 3 ++ documentation/js/while.js | 2 +- index.html | 52 +++++++++++++++++++++++++---- 8 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 documentation/coffee/slices.coffee create mode 100644 documentation/coffee/splices.coffee create mode 100644 documentation/js/slices.js create mode 100644 documentation/js/splices.js 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..ae83ce17 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,8 +1036,8 @@ 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) - by default ... it now parses 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.

diff --git a/documentation/js/comparisons.js b/documentation/js/comparisons.js index 4d4ccd85..13ad45a3 100644 --- a/documentation/js/comparisons.js +++ b/documentation/js/comparisons.js @@ -1,3 +1,3 @@ var cholesterol, healthy; cholesterol = 127; -healthy = 200 > cholesterol && cholesterol > 60; \ No newline at end of file +healthy = (200 > cholesterol && cholesterol > 60); \ No newline at end of file diff --git a/documentation/js/slices.js b/documentation/js/slices.js new file mode 100644 index 00000000..d3f451aa --- /dev/null +++ b/documentation/js/slices.js @@ -0,0 +1,4 @@ +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); \ No newline at end of file diff --git a/documentation/js/splices.js b/documentation/js/splices.js new file mode 100644 index 00000000..ed47b9b2 --- /dev/null +++ b/documentation/js/splices.js @@ -0,0 +1,3 @@ +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); \ No newline at end of file diff --git a/documentation/js/while.js b/documentation/js/while.js index fdcaa175..b8313867 100644 --- a/documentation/js/while.js +++ b/documentation/js/while.js @@ -3,7 +3,7 @@ if (this.studyingEconomics) { while (supply > demand) { buy(); } - while (supply <= demand) { + while (!(supply > demand)) { sell(); } } diff --git a/index.html b/index.html index f78c74d3..75c421e0 100644 --- a/index.html +++ b/index.html @@ -33,6 +33,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 @@ -804,7 +805,7 @@ lyrics = while num while (supply > demand) { buy(); } - while (supply <= demand) { + while (!(supply > demand)) { sell(); } } @@ -821,7 +822,7 @@ if (this.studyingEconomics) { while (supply > demand) { buy(); } - while (supply <= demand) { + while (!(supply > demand)) { sell(); } } @@ -944,6 +945,45 @@ ages = function() { 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). +

+
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

var cholesterol, healthy;
 cholesterol = 127;
-healthy = 200 > cholesterol && cholesterol > 60;
+healthy = (200 > cholesterol && cholesterol > 60);
 

+healthy = (200 > cholesterol && cholesterol > 60);;alert(healthy);'>run: healthy

@@ -1879,8 +1919,8 @@ task('build:parser