Coffeescript 简明教程
CoffeeScript - Ranges
在上一章中,我们已经看到了 CoffeeScript 中的数组,而在编程过程中,我们会面临一些场景其中,我们必须将一系列数值存储在一个数组中,如下所示。
In the previous chapter, we have seen Arrays in CoffeeScript, while programming we will face some scenarios where we have to store a sequence of numerical values in an array as shown below.
numbers =[1,2,3,4,5,6,7,8,9,10]
CoffeeScript 提供了一种更简洁的方法来表达包含一系列数值的数组,即 ranges 。CoffeeScript 的此功能源自 Ruby。
CoffeeScript provides a shorter way of expressing the arrays containing a sequence of numerical values, known as ranges. This feature of CoffeeScript is inspired from Ruby.
Syntax
范围由两个数值创建,范围中的第一个和最后一个位置,它们由 .. 或 … 分隔。使用两个点(1..4),范围是包含(1、2、3、4);使用三个点(1…4),范围不包括结尾(1、2、3)。
Ranges are created by two numerical values, the first and last positions in the range, separated by .. or …. With two dots (1..4), the range is inclusive (1, 2, 3, 4); with three dots (1…4), the range excludes the end (1, 2, 3).
下面给出 CoffeeScript 中范围的语法。我们将 square braces [ ] 中的范围内的数值定义,就像数组一样。在范围内,在存储一系列数值时,我们不必提供整个序列的值,我们只要按照下面所示,用两个点 .. 分隔其 begin 和 end 值即可。
Given below is the syntax of ranges in CoffeeScript. We will define the values in a range between square braces [ ] just like arrays. In ranges, while storing a sequence of numerical values, instead of providing the values of the whole sequence, we can just specify its begin and end values separated by two dots (..) as shown below.
range =[Begin..End]
Example
以下是 CoffeeScript 中范围的示例。将此内容保存到名为 ranges_example.coffee 的文件中。
Here is an example of ranges in CoffeeScript. Save this in a file with name ranges_example.coffee.
numbers =[0..9]
console.log "The contents of the range are: "+ numbers
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c ranges_example.coffee
在编译过程中,它会提供以下 JavaScript。在这里,你可以观察范围被转换为完整的 CoffeeScript 数组。
On compiling, it gives you the following JavaScript. Here you can observe that the range is converted in to complete CoffeeScript array.
// Generated by CoffeeScript 1.10.0
(function() {
var numbers;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log("The contents of the range are:: " + numbers);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee ranges_example.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
The contents of the range are:: 0,1,2,3,4,5,6,7,8,9
Excluding the end Value
范围被编译成包含所有数字的完整数组。如果我们想排除 end 值,那么我们必须用三个点 … 分隔范围的 start 和 end 元素,如下所示。
The ranges are compiled into complete arrays containing all numbers. If we want to exclude the end value, then we have to separate the start and end elements of the range using three dots (…) as shown below.
range =[Begin...End]
Example
我们可以通过排除 end 值来重写上述示例,如下所示。将以下内容保存到名为 range_excluding_end.coffee 的文件中。
We can rewrite the above example by excluding the end value as shown below. Save the following contents in a file with name range_excluding_end.coffee
numbers =[0...9]
console.log "The contents of the range are:: "+ numbers
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c ranges_example.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var numbers;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8];
console.log("The contents of the range are:: " + numbers);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee ranges_example.coffee
在执行时,CoffeeScript 文件会产生以下输出。在这里,你可以观察到结尾值 9 被排除在外。
On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.
The contents of the range are:: 0,1,2,3,4,5,6,7,8
Using Ranges with Variables
我们还可以通过将 start 和 end 值赋值给变量来定义范围。
We can also define a range by assigning the start and end values to variables.
Example
考虑以下示例。在这里,我们使用变量定义了范围。将此代码保存到名为 range_variables.coffee 的文件中。
Consider the following example. Here we have defined a range using variables. Save this code in a file with name range_variables.coffee
start=0
end=9
numbers =[start..end]
console.log "The contents of the range are: "+ numbers
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c range_variables.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var end, i, numbers, results, start;
start = 0;
end = 9;
numbers = (function() {
results = [];
for (var i = start; start <= end ? i <= end : i >= end; start <= end ? i++ : i--) {
results.push(i);
}
return results;
}).apply(this);
console.log("The contents of the range are:: " + numbers);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee range_variables.coffee
在执行时,CoffeeScript 文件会产生以下输出。在这里,你可以观察到结尾值 9 被排除在外。
On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.
The contents of the range are:: 0,1,2,3,4,5,6,7,8,9
Ranges with Arrays
我们可以通过将其与范围结合使用来分割数组。每当我们在数组(变量)之后立即指定范围时,CoffeeScript 编译器会将其转换为 JavaScript 的 slice() 方法调用。
We can slice arrays by using them with ranges. Whenever we specify ranges immediately after arrays (variables), then the CoffeeScript compiler converts it in to a slice() method call of JavaScript.
假设我们有一个包含数值的数组(0 到 9),那么我们可以像下面这样检索它的前 4 个元素。
Assume that we have an array having numerical values, say 0 to 9, then we can retrieve the first 4 elements of it as shown below.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data = num[0..5]
负值表示从后往前数的元素数量,例如,-1 表示 9。如果我们指定一个负数 3 后面跟着两个冒号,则将提取该数组的最后三个元素。
Negative values represent the elements from the end, for example, -1 indicates 9. If we specify a negative number 3 followed by two dots, the last three elements of the array will be extracted.
data = num[-3..]
如果我们只指定数组范围中两个冒号 num[..] ,则将提取完整数组。我们还可以使用范围替换数组片段,如下所示。
If we specify only two dots in the range of an array as num[..], then the complete array will be extracted. We can also replace an array segment with other elements using ranges as shown below.
num[2..6] = [13,14,15,16,17]
Example
以下示例演示了使用数组范围。将这段代码保存到名为 range_arrays.coffee 的文件中。
The following example demonstrates the use of ranges with arrays. Save this code in a file with name range_arrays.coffee
#slicing an array using ranges
num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data = num[0..5]
console.log "The first four elements of the array : "+data
#Using negative values
data = num[-3..]
console.log "The last 3 elements of the array : "+data
#Extracting the whole array
console.log "Total elements of the array : "+num[..]
#Replacing the elements of an array
num[2..6] = [13,14,15,16,17]
console.log "New array : "+num
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c range_arrays.coffee
编译后,它会输出以下 JavaScript。在这里,你可以看到所有范围都转换为 JavaScript 的 slice() 方法调用。
On compiling, it gives you the following JavaScript. Here you can observe that all the ranges are converted in to the slice() method calls of JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var data, num, ref;
num = [1, 2, 3, 4, 5, 6, 7, 8, 9];
data = num.slice(0, 6);
console.log("The first four elements of the array : " + data);
data = num.slice(-3);
console.log("The last 3 elements of the array : " + data);
console.log("Total elements of the array : " + num.slice(0));
[].splice.apply(num, [2, 5].concat(ref = [13, 14, 15, 16, 17])), ref;
console.log("New array : " + num);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee range_arrays.coffee
在执行时,CoffeeScript 文件会产生以下输出。在这里,你可以观察到结尾值 9 被排除在外。
On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.
The first four elements of the array : 1,2,3,4,5,6
The last 3 elements of the array : 7,8,9
Total elements of the array : 1,2,3,4,5,6,7,8,9
New array : 1,2,13,14,15,16,17,8,9
Ranges with Strings
我们还可以对字符串使用范围。如果我们在字符串后指定范围,则 CoffeeScript 会对它们进行切片并返回一个新的字符子集。
We can also use ranges with Strings. If we specify ranges after Strings, then CoffeeScript slices them and returns a new subset of characters.
Example
以下示例演示了使用字符串范围。在这里,我们创建了一个字符串,并使用范围从其中提取了一个子字符串。将这段代码保存到名为 ranges_with_strings.coffee 的文件中。
The following example demonstrates the use of ranges with Strings. Here we have created a string and extracted a substring from it using ranges. Save this code in a file with name ranges_with_strings.coffee
my_string = "Welcome to tutorialspoint"
new_string = my_string[0..10]
console.log new_string
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c ranges_with_strings.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var my_string, new_string;
my_string = "Welcome to tutorialspoint";
new_string = my_string.slice(0, 6);
console.log(new_string);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee ranges_with_strings.coffee
在执行时,CoffeeScript 文件会产生以下输出。在这里,你可以观察到结尾值 9 被排除在外。
On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.
Welcome to
Comprehensions over Ranges
像对象和数组一样,我们还可以使用解析器迭代一个范围的元素。
As objects and arrays, we can also iterate the elements of a range using comprehensions.
Example
以下是使用解析器对范围进行解析的一个示例。在这里,我们创建了一个范围,并使用解析器检索了其中的元素。将这段代码保存到名为 comprehensions_over_ranges.coffee 的文件中。
Following is an example of using comprehensions over ranges. Here we have created a range and retrieved the elements in it using comprehensions. Save this code in a file with the name comprehensions_over_ranges.coffee
numbers =[0..9]
console.log "The elements of the range are: "
console.log num for num in numbers
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c comprehensions_over_ranges.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var i, len, num, numbers;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log("The elements of the range are: ");
for (i = 0, len = numbers.length; i < len; i++) {
num = numbers[i];
console.log(num);
}
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee comprehensions_over_ranges.coffee
在执行时,CoffeeScript 文件会产生以下输出。在这里,你可以观察到结尾值 9 被排除在外。
On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.
The elements of the range are:
0
1
2
3
4
5
6
7
8
同样,我们还可以使用解析的 by 关键字更改此递增值。
In the same way We can also change this increment using the by keyword of comprehensions.
array = (num for num in [1..10] by 2)
console.log array