CoffeeScript - Ranges



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 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

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).

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

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 

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c ranges_example.coffee

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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee ranges_example.coffee

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

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

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 

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c ranges_example.coffee

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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee ranges_example.coffee

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

We can also define a range by assigning the start and end values to variables.

Example

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

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c range_variables.coffee

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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee range_variables.coffee

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

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.

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]

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..]

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

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 

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c range_arrays.coffee

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); 

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee range_arrays.coffee

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

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

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

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c ranges_with_strings.coffee

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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee ranges_with_strings.coffee

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

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

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c comprehensions_over_ranges.coffee

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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee comprehensions_over_ranges.coffee

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

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
Advertisements