Coffeescript-ranges

提供:Dev Guides
移動先:案内検索

CoffeeScript-範囲

前の章で、CoffeeScriptで配列を見てきましたが、プログラミング中に、以下に示すように配列に数値のシーケンスを格納する必要があるいくつかのシナリオに直面します。

numbers =[1,2,3,4,5,6,7,8,9,10]

CoffeeScriptは、 ranges と呼ばれる一連の数値を含む配列を表現する簡単な方法を提供します。 CoffeeScriptのこの機能は、Rubyから着想を得ています。

構文

範囲は、。で区切られた範囲内の最初と最後の位置の2つの数値によって作成されます。 または…​. 2つのドット(1..4)では、範囲は包括的(1、2、3、4)です。 3つのドット(1 …​ 4)で、範囲は終了(1、2、3)を除外します。

以下に、CoffeeScriptの範囲の構文を示します。 配列と同様に、角カッコ [] の間の範囲で値を定義します。 範囲内では、数値のシーケンスを格納する際に、シーケンス全体の値を提供する代わりに、以下に示すように、2つのドット( .. )で区切られた begin および end 値を指定できます。

range =[Begin..End]

CoffeeScriptの範囲の例を次に示します。 これを ranges_example.coffee という名前のファイルに保存します。

numbers =[0..9]
console.log "The contents of the range are: "+ numbers
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c ranges_example.coffee

コンパイル時に、次のJavaScriptが提供されます。 ここで、範囲が完全なCoffeeScript配列に変換されることがわかります。

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

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee ranges_example.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The contents of the range are:: 0,1,2,3,4,5,6,7,8,9

最終値を除く

範囲は、すべての数値を含む完全な配列にコンパイルされます。 end 値を除外する場合、以下に示すように3つのドット( …​ )を使用して範囲の start および end 要素を分離する必要があります。

range =[Begin...End]

以下に示すように、 end 値を除外することにより、上記の例を書き換えることができます。 次のコンテンツを range_exexcept_end.coffee という名前のファイルに保存します

numbers =[0...9]
console.log "The contents of the range are:: "+ numbers
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c ranges_example.coffee

コンパイル時に、次の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);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee ranges_example.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。 ここでは、終了値 9 が除外されていることがわかります。

The contents of the range are:: 0,1,2,3,4,5,6,7,8

変数で範囲を使用する

開始値と終了値を変数に割り当てることにより、範囲を定義することもできます。

次の例を考えてください。 ここで、変数を使用して範囲を定義しました。 このコードを range_variables.coffee という名前のファイルに保存します

start=0
end=9
numbers =[start..end]
console.log "The contents of the range are: "+ numbers
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c range_variables.coffee

コンパイル時に、次の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);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee range_variables.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。 ここでは、終了値 9 が除外されていることがわかります。

The contents of the range are:: 0,1,2,3,4,5,6,7,8,9

配列のある範囲

範囲を使用して配列をスライスできます。 配列(変数)の直後に範囲を指定すると、CoffeeScriptコンパイラーはそれをJavaScriptの* slice()*メソッド呼び出しに変換します。

0〜9などの数値を持つ配列があるとします。次に示すように、最初の4つの要素を取得できます。

num  = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data = num[0..5]

負の値は、末尾からの要素を表します。たとえば、-1は9を示します。 負の数3の後に2つのドットを指定すると、配列の最後の3つの要素が抽出されます。

data = num[-3..]

配列の範囲内で2つのドットのみを num [..] として指定すると、完全な配列が抽出されます。 以下に示す範囲を使用して、配列セグメントを他の要素に置き換えることもできます。

num[2..6] = [13,14,15,16,17]

次の例は、配列での範囲の使用を示しています。 このコードを 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
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c range_arrays.coffee

コンパイル時に、次のJavaScriptが提供されます。 ここで、すべての範囲がJavaScriptのslice()メソッド呼び出しに変換されることを確認できます。

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

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee range_arrays.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。 ここでは、終了値 9 が除外されていることがわかります。

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

文字列のある範囲

文字列で範囲を使用することもできます。 Stringsの後に範囲を指定すると、CoffeeScriptは範囲をスライスし、文字の新しいサブセットを返します。

次の例は、文字列での範囲の使用を示しています。 ここで、文字列を作成し、範囲を使用してそこから部分文字列を抽出しました。 このコードを ranges_with_strings.coffee という名前のファイルに保存します

my_string = "Welcome to finddevguides"
new_string = my_string[0..10]
console.log new_string
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c ranges_with_strings.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var my_string, new_string;

  my_string = "Welcome to finddevguides";

  new_string = my_string.slice(0, 6);

  console.log(new_string);

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee ranges_with_strings.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。 ここでは、終了値 9 が除外されていることがわかります。

Welcome to

範囲にわたる理解

オブジェクトおよび配列として、内包表記を使用して範囲の要素を繰り返すこともできます。

以下は、範囲に対する内包表記の使用例です。 ここで、範囲を作成し、内包表記を使用してその中の要素を取得しました。 このコードを comprehensions_over_ranges.coffee という名前のファイルに保存します

numbers =[0..9]
console.log "The elements of the range are: "
console.log num for num in numbers
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c comprehensions_over_ranges.coffee

コンパイル時に、次の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);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee comprehensions_over_ranges.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。 ここでは、終了値 9 が除外されていることがわかります。

The elements of the range are:
0
1
2
3
4
5
6
7
8

同様に、内包表記のbyキーワードを使用してこの増分を変更することもできます。

array = (num for num in [1..10] by 2)
console.log array