Coffeescript-comprehensions

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

CoffeeScript-理解

前の章で、CoffeeScriptが提供するさまざまなループ、 while およびそのバリアントを学習しました。 それらに加えて、CoffeeScriptは comprehensions として知られる追加のループ構造を提供します。

オプションのガード句と現在の配列インデックスの値を明示的に追加すると、これらの内包表記は他のプログラミング言語の for ループを置き換えます。 内包表記を使用すると、配列だけでなくオブジェクトを反復することができ、配列を反復する内包表記は式であり、関数で返すか、変数に割り当てることができます。

S.No. Statement & Description
1

for..in comprehensions

  • for..in* 内包表記はCoffeeScriptの内包表記の基本形式で、これを使用してリストまたは配列の要素を反復処理できます。
2

for..of comprehensions

配列のように、CoffeeScriptScriptは、オブジェクトと呼ばれるキーと値のペアを保存するためのコンテナーを提供します。 CoffeeScriptが提供する for..of 内包表記を使用してオブジェクトを反復できます。

3

list comprehensions

CoffeeScriptの list 内包表記は、オブジェクトの配列を別の配列にマッピングするために使用されます。

理解の指標

要素のリスト/配列には、内包表記で使用できるインデックスがあります。 以下に示すように、変数を使用して内包表記で使用できます。

for student,i in [element1, element2, element3]

次の例は、CoffeeScriptの for…in 内包表記のインデックスの使用方法を示しています。 このコードを for_in_index.coffee という名前のファイルに保存します

for student,i in ['Ram', 'Mohammed', 'John']
   console.log "The name of the student with id "+i+" is: "+student
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c for_in_index.coffee

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

//Generated by CoffeeScript 1.10.0
(function() {
  var i, j, len, ref, student;

  ref = ['Ram', 'Mohammed', 'John'];
  for (i = j = 0, len = ref.length; j < len; i = ++j) {
    student = ref[i];
    console.log("The name of the student with id " + i + " is: " + student);
  }
}).call(this);

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

c:\> coffee for_in_index.coffee

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

The name of the student with id 0 is: Ram
The name of the student with id 1 is: Mohammed
The name of the student with id 2 is: John

後置形式の内包表記

接尾辞 if および unless と同様に、CoffeeScriptは、コードの作成中に便利なComprehensionsの接尾辞形式を提供します。 これを使用して、次のように for..in 内包表記を1行で記述できます。

#Postfix for..in comprehension
console.log student for student in ['Ram', 'Mohammed', 'John']

#postfix for..of comprehension
console.log key+"::"+value for key,value of { name: "Mohammed", age: 24, phone: 9848022338}

リンク:/coffeescript/postfix_form_of_comprehensions [例を表示]

変数への割り当て

配列を反復するために使用する内包表記は変数に割り当てられ、関数によって返されます。

以下の例を考えてみましょう。 ここでは、 for..in 内包表記を使用して配列の要素を取得し、これを names という名前の変数に割り当てたことを確認できます。 また、 return キーワードを使用して明示的に内包表記を返す関数もあります。 このコードを example.coffee という名前のファイルに保存します

my_function =->
   student = ['Ram', 'Mohammed', 'John']

   #Assigning comprehension to a variable
   names = (x for x in student )
   console.log "The contents of the variable names are ::"+names

   #Returning the comprehension
   return x for x in student
console.log "The value returned by the function is "+my_function()
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c example.coffee

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

//Generated by CoffeeScript 1.10.0
(function() {
  var my_function;

  my_function = function() {
    var i, len, names, student, x;
    student = ['Ram', 'Mohammed', 'John'];
    names = (function() {
      var i, len, results;
      results = [];
      for (i = 0, len = student.length; i < len; i++) {
        x = student[i];
        results.push(x);
      }
      return results;
    })();
    console.log("The contents of the variable names are ::" + names);
    for (i = 0, len = student.length; i < len; i++) {
      x = student[i];
      return x;
    }
  };

  console.log("The value returned by the function is " + my_function());

}).call(this);

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

c:\> coffee example.coffee

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

The contents of the variable names are ::Ram,Mohammed,John
The value returned by the function is Ram

byキーワード

CoffeeScriptは、要素のリストを定義する範囲を提供します。 たとえば、範囲[1..10]は[1、2、3、4、5、6、7、8、9、10]と同等であり、すべての要素が1ずつ増加します。 内包表記の by キーワードを使用して、この増分を変更することもできます。

次の例は、CoffeeScriptが提供する for..in 内包表記の by キーワードの使用方法を示しています。 このコードを by_keyword_example.coffee という名前のファイルに保存します

array = (num for num in [1..10] by 2)
console.log array
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c by_keyword_example.coffee

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

//Generated by CoffeeScript 1.10.0
(function() {
  var array, num;

  array = (function() {
    var i, results;
    results = [];
    for (num = i = 1; i <= 10; num = i += 2) {
      results.push(num);
    }
    return results;
  })();

  console.log(array);

}).call(this);

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

c:\> coffee by_keyword_example.coffee

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

[ 1, 3, 5, 7, 9 ]