アットウィキロゴ

Chapter 10 For-Comprehensions

「Chapter 10 For-Comprehensions」の編集履歴(バックアップ)一覧はこちら

Chapter 10 For-Comprehensions - (2011/02/24 (木) 08:48:20) の1つ前との変更点

追加された行は緑色になります。

削除された行は赤色になります。

#co(){ Chapter 10 For-Comprehensions The last chapter demonstrated that higher-order functions such as map, flatMap, filter provide powerful constructions for dealing with lists. But sometimes the level of abstraction required by these functions makes a program hard to understand. } * 第 10 章 For 内包表記 (For-Comprehensions) 前の章では map, flatMap, filter のような高階関数がリストを扱う強力な道具となることを示しました。しかし時に、これらの関数が要求する抽象化レベルゆえに、プログラムが理解しにくくなることもあります。 #co(){ To help understandability, Scala has a special notation which simplifies common patterns of applications of higher-order functions. This notation builds a bridge between set-comprehensions in mathematics and for-loops in imperative languages such as C or Java. It also closely resembles the query notation of relational databases. } 理解しやすくするために、 Scala には高階関数適用の共通パターンを簡単にする特別な記法があります。この記法は数学における集合の内包表記と、C や Java のような命令型言語の for ループとの架け橋となるものです。また関係データベースのクエリ(問い合わせ)記法にもよく似ています。 #co(){ As a first example, say we are given a list persons of persons with name and age fields. To print the names of all persons in the sequence which are aged over 20, one can write: } 最初の例として、name と age をフィールドにもつ人(person)のリスト persons があるとしましょう。そのリストの中で、年齢が 20 を超える人の名前を表示するのは、次のように書けます。 for (p <- persons if p.age > 20) yield p.name #co(){ This is equivalent to the following expression , which uses higher-order functions filter and map: } これは高階関数 filter と map を使った次の式と等価です。 persons filter (p => p.age > 20) map (p => p.name) #co(){ The for-comprehension looks a bit like a for-loop in imperative languages, except that it constructs a list of the results of all iterations. } for 内包表記は命令型言語における for ループにすこし似ていますが、各繰り返しの結果値をすべて集めてリストを作る点が違います。 #co(){ Generally, a for-comprehension is of the form } 一般的に、for 内包表記は次の形式です。 for ( s ) yield e #co(){ Here, s is a sequence of generators, definitions and filters. A generator is of the form val x <- e, where e is a list-valued expression. It binds x to successive values in the list. A definition is of the form val x = e. It introduces x as a name for the value of e in the rest of the comprehension. A filter is an expression f of type Boolean. It omits from consideration all bindings for which f is false. The sequence s starts in each case with a generator. If there are several generators in a sequence, later generators vary more rapidly than earlier ones. } ここで s は&bold(){ジェネレータ}、&bold(){定義}、&bold(){フィルタ}のシーケンス(並び)です。&bold(){ジェネレータ}とは val x <- e という形 (ただし e は値がリストとなる式) です。それによって x はリスト中の値で次々に束縛されます。&bold(){定義}とは、val x = e という形です。残りの内包表記において、x は e の値の名前として導入されます。&bold(){フィルタ}とは Boolean 型の式 f です。f が false となるような束縛はすべて無視されます。シーケンス s は常にジェネレータで始まります。もしいくつかのジェネレータがシーケンスに含まれる場合、後にあるジェネレータは前にあるものよりも先に変化します。 #co(){ The sequence s may also be enclosed in braces instead of parentheses, in which case the semicolons between generators, definitions and filters can be omitted. } シーケンス s は丸括弧 () ではなく波括弧 {} で囲むこともでき、その場合はジェネレータ、定義、フィルタの間のセミコロンは省略できます。 #co(){ Here are two examples that show how for-comprehensions are used. First, let's redo an example of the previous chapter: Given a positive integer n, find all pairs of positive integers i and j , where 1 ≤ j < i < n such that i + j is prime. With a for-comprehension this problem is solved as follows: } for 内包表記を使う例を二つ示します。最初に、前の章の例を繰り返しましょう。与えられた正整数 n に対し、正整数 i と j のすべての組 (ただし 1 <= j < i < n で、i+j が素数)を求めなさい。for 内包表記を使うと、この問題は次のように解けます。 for { i <- List.range(1, n) j <- List.range(1, i) if isPrime(i+j) } yield {i, j} #co(){ This is arguably much clearer than the solution using map, flatMap and filter that we have developed previously. } 前に考えた map, flatMap, filter を使った解よりずっと明快であるのは間違いないでしょう。 #co(){ As a second example, consider computing the scalar product of two vectors xs and ys. Using a for-comprehension, this can be written as follows. } 二つ目の例として、2つのベクトル xs と ys のスカラー積の計算を考えましょう。for 内包表記を使って次のように書けます。 sum(for ((x, y) <- xs zip ys) yield x * y) - [[10.1 N クィーン問題 (The N-Queens Problem) >Example10.1]] - [[10.2 For 内包表記によるクエリ >Example10.2]] - [[10.3 For 内包表記の変換 >Example10.3]] - [[10.4 For ループ >Example10.4]] - [[10.5 For の一般化 (Generalizing For) >Example10.5]] #center(){[[前ページ>Example9.5]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.1]]} ---- #comment
#co(){ Chapter 10 For-Comprehensions The last chapter demonstrated that higher-order functions such as map, flatMap, filter provide powerful constructions for dealing with lists. But sometimes the level of abstraction required by these functions makes a program hard to understand. } #setmenu2(ex-r-menu) * 第 10 章 For 内包表記 (For-Comprehensions) 前の章では map, flatMap, filter のような高階関数がリストを扱う強力な道具となることを示しました。しかし時に、これらの関数が要求する抽象化レベルゆえに、プログラムが理解しにくくなることもあります。 #co(){ To help understandability, Scala has a special notation which simplifies common patterns of applications of higher-order functions. This notation builds a bridge between set-comprehensions in mathematics and for-loops in imperative languages such as C or Java. It also closely resembles the query notation of relational databases. } 理解しやすくするために、 Scala には高階関数適用の共通パターンを簡単にする特別な記法があります。この記法は数学における集合の内包表記と、C や Java のような命令型言語の for ループとの架け橋となるものです。また関係データベースのクエリ(問い合わせ)記法にもよく似ています。 #co(){ As a first example, say we are given a list persons of persons with name and age fields. To print the names of all persons in the sequence which are aged over 20, one can write: } 最初の例として、name と age をフィールドにもつ人(person)のリスト persons があるとしましょう。そのリストの中で、年齢が 20 を超える人の名前を表示するのは、次のように書けます。 for (p <- persons if p.age > 20) yield p.name #co(){ This is equivalent to the following expression , which uses higher-order functions filter and map: } これは高階関数 filter と map を使った次の式と等価です。 persons filter (p => p.age > 20) map (p => p.name) #co(){ The for-comprehension looks a bit like a for-loop in imperative languages, except that it constructs a list of the results of all iterations. } for 内包表記は命令型言語における for ループにすこし似ていますが、各繰り返しの結果値をすべて集めてリストを作る点が違います。 #co(){ Generally, a for-comprehension is of the form } 一般的に、for 内包表記は次の形式です。 for ( s ) yield e #co(){ Here, s is a sequence of generators, definitions and filters. A generator is of the form val x <- e, where e is a list-valued expression. It binds x to successive values in the list. A definition is of the form val x = e. It introduces x as a name for the value of e in the rest of the comprehension. A filter is an expression f of type Boolean. It omits from consideration all bindings for which f is false. The sequence s starts in each case with a generator. If there are several generators in a sequence, later generators vary more rapidly than earlier ones. } ここで s は&bold(){ジェネレータ}、&bold(){定義}、&bold(){フィルタ}のシーケンス(並び)です。&bold(){ジェネレータ}とは val x <- e という形 (ただし e は値がリストとなる式) です。それによって x はリスト中の値で次々に束縛されます。&bold(){定義}とは、val x = e という形です。残りの内包表記において、x は e の値の名前として導入されます。&bold(){フィルタ}とは Boolean 型の式 f です。f が false となるような束縛はすべて無視されます。シーケンス s は常にジェネレータで始まります。もしいくつかのジェネレータがシーケンスに含まれる場合、後にあるジェネレータは前にあるものよりも先に変化します。 #co(){ The sequence s may also be enclosed in braces instead of parentheses, in which case the semicolons between generators, definitions and filters can be omitted. } シーケンス s は丸括弧 () ではなく波括弧 {} で囲むこともでき、その場合はジェネレータ、定義、フィルタの間のセミコロンは省略できます。 #co(){ Here are two examples that show how for-comprehensions are used. First, let's redo an example of the previous chapter: Given a positive integer n, find all pairs of positive integers i and j , where 1 ≤ j < i < n such that i + j is prime. With a for-comprehension this problem is solved as follows: } for 内包表記を使う例を二つ示します。最初に、前の章の例を繰り返しましょう。与えられた正整数 n に対し、正整数 i と j のすべての組 (ただし 1 <= j < i < n で、i+j が素数)を求めなさい。for 内包表記を使うと、この問題は次のように解けます。 for { i <- List.range(1, n) j <- List.range(1, i) if isPrime(i+j) } yield {i, j} #co(){ This is arguably much clearer than the solution using map, flatMap and filter that we have developed previously. } 前に考えた map, flatMap, filter を使った解よりずっと明快であるのは間違いないでしょう。 #co(){ As a second example, consider computing the scalar product of two vectors xs and ys. Using a for-comprehension, this can be written as follows. } 二つ目の例として、2つのベクトル xs と ys のスカラー積の計算を考えましょう。for 内包表記を使って次のように書けます。 sum(for ((x, y) <- xs zip ys) yield x * y) - [[10.1 N クィーン問題 (The N-Queens Problem) >Example10.1]] - [[10.2 For 内包表記によるクエリ >Example10.2]] - [[10.3 For 内包表記の変換 >Example10.3]] - [[10.4 For ループ >Example10.4]] - [[10.5 For の一般化 (Generalizing For) >Example10.5]] #center(){[[前ページ>Example9.5]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.1]]} ---- #comment

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。