From dd5317ebe901d1fde6e08519359f19759963338f Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 25 Dec 2009 16:35:57 -0800 Subject: [PATCH] documenting ranges and slices --- documentation/coffee/slices.coffee | 8 +++- documentation/index.html.erb | 25 ++++++----- documentation/js/slices.js | 5 ++- index.html | 43 +++++++++++-------- .../execution/test_ranges_and_slices.coffee | 8 ++++ 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/documentation/coffee/slices.coffee b/documentation/coffee/slices.coffee index ac34b0b2..7136e954 100644 --- a/documentation/coffee/slices.coffee +++ b/documentation/coffee/slices.coffee @@ -1,2 +1,6 @@ -nums: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -three_to_six: nums[3, 6] \ No newline at end of file +numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +three_to_six: numbers[3..6] + +numbers_copy: numbers[0...numbers.length] + diff --git a/documentation/index.html.erb b/documentation/index.html.erb index 3ffd8e28..c68e78b4 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -64,7 +64,7 @@ Aliases
While Loops
Array Comprehensions
- Array Slice Literals
+ Slicing Arrays with Ranges
Inheritance, and Calling Super from a Subclass
Embedded JavaScript
Switch/When/Else
@@ -349,12 +349,15 @@ coffee-script --print app/scripts/*.coffee > concatenation.js <%= code_for('array_comprehensions') %>

- Array Slice Literals - CoffeeScript includes syntax for extracting slices of arrays. - The first argument is the index of the first element in the slice, and - the second is the index of the last one. + Slicing Arrays with Ranges + CoffeeScript borrows Ruby's + range syntax + for extracting slices of arrays. With two dots (3..5), the range + is inclusive: the first argument is the index of the first element in + the slice, and the second is the index of the last one. Three dots signify + a range that excludes the end.

- <%= code_for('slices', 'three_to_six') %> + <%= code_for('slices', 'numbers_copy') %>

Inheritance, and Calling Super from a Subclass @@ -443,17 +446,17 @@ coffee-script --print app/scripts/*.coffee > concatenation.js

Change Log

- +

0.1.4 The official CoffeeScript extension is now .coffee instead of - .cs, which properly belongs to + .cs, which properly belongs to C#. Due to popular demand, you can now also use = to assign. Unlike JavaScript, = can also be used within object literals, interchangeably - with :. Made a grammatical fix for chained function calls - like func(1)(2)(3)(4). Inheritance and super no longer use - __proto__, so they should be IE-compatible now. + with :. Made a grammatical fix for chained function calls + like func(1)(2)(3)(4). Inheritance and super no longer use + __proto__, so they should be IE-compatible now.

diff --git a/documentation/js/slices.js b/documentation/js/slices.js index a82ecdbe..5f590283 100644 --- a/documentation/js/slices.js +++ b/documentation/js/slices.js @@ -1,4 +1,5 @@ (function(){ - var nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - var three_to_six = nums.slice(3, 6 + 1); + var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + var three_to_six = numbers.slice(3, 6 + 1); + var numbers_copy = numbers.slice(0, numbers.length); })(); \ No newline at end of file diff --git a/index.html b/index.html index bfe0380b..437fd44d 100644 --- a/index.html +++ b/index.html @@ -50,7 +50,7 @@ Aliases
While Loops
Array Comprehensions
- Array Slice Literals
+ Slicing Arrays with Ranges
Inheritance, and Calling Super from a Subclass
Embedded JavaScript
Switch/When/Else
@@ -578,18 +578,27 @@ __h;

- Array Slice Literals - CoffeeScript includes syntax for extracting slices of arrays. - The first argument is the index of the first element in the slice, and - the second is the index of the last one. + Slicing Arrays with Ranges + CoffeeScript borrows Ruby's + range syntax + for extracting slices of arrays. With two dots (3..5), the range + is inclusive: the first argument is the index of the first element in + the slice, and the second is the index of the last one. Three dots signify + a range that excludes the end.

-
nums: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-three_to_six: nums[3, 6]
-
var nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
-var three_to_six = nums.slice(3, 6 + 1);
-

+
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+three_to_six: numbers[3..6]
+
+numbers_copy: numbers[0...numbers.length]
+
+
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
+var three_to_six = numbers.slice(3, 6 + 1);
+var numbers_copy = numbers.slice(0, numbers.length);
+

Inheritance, and Calling Super from a Subclass @@ -832,17 +841,17 @@ world...";

Change Log

- +

0.1.4 The official CoffeeScript extension is now .coffee instead of - .cs, which properly belongs to + .cs, which properly belongs to C#. Due to popular demand, you can now also use = to assign. Unlike JavaScript, = can also be used within object literals, interchangeably - with :. Made a grammatical fix for chained function calls - like func(1)(2)(3)(4). Inheritance and super no longer use - __proto__, so they should be IE-compatible now. + with :. Made a grammatical fix for chained function calls + like func(1)(2)(3)(4). Inheritance and super no longer use + __proto__, so they should be IE-compatible now.

diff --git a/test/fixtures/execution/test_ranges_and_slices.coffee b/test/fixtures/execution/test_ranges_and_slices.coffee index e69de29b..63165c5d 100644 --- a/test/fixtures/execution/test_ranges_and_slices.coffee +++ b/test/fixtures/execution/test_ranges_and_slices.coffee @@ -0,0 +1,8 @@ +array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +a: array[7..9] +b: array[2...4] + +result: a.concat(b).join(' ') + +print(result is "7 8 9 2 3") \ No newline at end of file