「Example10.3」の編集履歴(バックアップ)一覧はこちら
「Example10.3」(2011/02/24 (木) 08:50:19) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
#co(){
10.3 Translation of For-Comprehensions
Every for-comprehension can be expressed in terms of the three higher-order functions map, flatMap and filter. Here is the translation scheme, which is also used by the Scala compiler.
}
** 10.3 For 内包表記の変換
すべての for 内包表記は3つの高階関数 map, flatMap, filter で表現できます。これが翻訳のスキーム(枠組み)であり、Scala コンパイラも使っています。
#co(){
• A simple for-comprehension
}
• 単純な for 内包表記
for (x <- e) yield e'
#co(){
is translated to
}
これは次のように翻訳されます。
e.map(x => e')
#co(){
• A for-comprehension
}
• for 内包表記
for (x <- e if f; s) yield e'
#co(){
where f is a filter and s is a (possibly empty) sequence of generators or filters is translated to
}
ただし f はフィルタで、s は (空でも良い) ジェネレータあるいはフィルタの列。これは次のように翻訳されます。
for (x <- e.filter(x => f); s) yield e'
#co(){
and then translation continues with the latter expression.
}
そして後者の式に対して翻訳が続きます。
#co(){
• A for-comprehension
}
• for 内包表記
for (x <- e; y <- e'; s) yield e''
#co(){
where s is a (possibly empty) sequence of generators or filters is translated to
}
ただし s は (空でも良い) ジェネレータあるいはフィルタの列。これは次のように翻訳されます。
e.flatMap(x => for (y <- e'; s) yield e'')
#co(){
and then translation continues with the latter expression.
}
そして後者の式に対して翻訳が続きます。
#co(){
For instance, taking our "pairs of integers whose sum is prime" example:
}
たとえば、「和が素数になる整数の組」を例にとると、
for { i <- range(1, n)
j <- range(1, i)
if isPrime(i+j)
} yield {i, j}
#co(){
Here is what we get when we translate this expression:
}
この式を翻訳すると次が得られます。
range(1, n)
.flatMap(i =>
range(1, i)
.filter(j => isPrime(i+j))
.map(j => (i, j)))
#co(){
Conversely, it would also be possible to express functions map, flatMap and filter using for-comprehensions. Here are the three functions again, this time implemented using for-comprehensions.
}
逆に、関数 map, flatMap, filter を for 内包表記で表すこともできます。3つの関数を今度は for 内包表記を使って実装してみます。
object Demo {
def map[A, B](xs: List[A], f: A => B): List[B] =
for (x <- xs) yield f(x)
def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] =
for (x <- xs; y <- f(x)) yield y
def filter[A](xs: List[A], p: A => Boolean): List[A] =
for (x <- xs if p(x)) yield x
}
#co(){
Not surprisingly, the translation of the for-comprehension in the body of Demo.map will produce a call to map in class List. Similarly, Demo.flatMap and Demo.filter translate to flatMap and filter in class List.
}
驚くことではありませんが、Demo.map の本体の for 内包表記の翻訳は、クラス List の map を呼び出します。同様に、Demo.flatMap と Demo.filter は、クラス List の flatMap と filterの呼び出しへ翻訳されます。
#co(){
Exercise 10.3.1 Define the following function in terms of for.
}
&b(){演習 10.3.1 } 次の関数を for を用いて定義しなさい。
def flatten[A](xss: List[List[A]]): List[A] =
(xss :\ (Nil: List[A])) ((xs, ys) => xs ::: ys)
#co(){
Exercise 10.3.2 Translate
}
&b(){演習 10.3.2 } 次を高階関数を用いて翻訳しなさい。
for (b <- books; a <- b.authors if a startsWith "Bird") yield b.title
for (b <- books if (b.title indexOf "Program") >= 0) yield b.title
#co(){
to higher-order functions.
}
#center(){[[前ページ>Example10.2]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.4]]}
----
#comment
#co(){
10.3 Translation of For-Comprehensions
Every for-comprehension can be expressed in terms of the three higher-order functions map, flatMap and filter. Here is the translation scheme, which is also used by the Scala compiler.
}
#setmenu2(ex-r-menu)
** 10.3 For 内包表記の変換
すべての for 内包表記は3つの高階関数 map, flatMap, filter で表現できます。これが翻訳のスキーム(枠組み)であり、Scala コンパイラも使っています。
#co(){
• A simple for-comprehension
}
• 単純な for 内包表記
for (x <- e) yield e'
#co(){
is translated to
}
これは次のように翻訳されます。
e.map(x => e')
#co(){
• A for-comprehension
}
• for 内包表記
for (x <- e if f; s) yield e'
#co(){
where f is a filter and s is a (possibly empty) sequence of generators or filters is translated to
}
ただし f はフィルタで、s は (空でも良い) ジェネレータあるいはフィルタの列。これは次のように翻訳されます。
for (x <- e.filter(x => f); s) yield e'
#co(){
and then translation continues with the latter expression.
}
そして後者の式に対して翻訳が続きます。
#co(){
• A for-comprehension
}
• for 内包表記
for (x <- e; y <- e'; s) yield e''
#co(){
where s is a (possibly empty) sequence of generators or filters is translated to
}
ただし s は (空でも良い) ジェネレータあるいはフィルタの列。これは次のように翻訳されます。
e.flatMap(x => for (y <- e'; s) yield e'')
#co(){
and then translation continues with the latter expression.
}
そして後者の式に対して翻訳が続きます。
#co(){
For instance, taking our "pairs of integers whose sum is prime" example:
}
たとえば、「和が素数になる整数の組」を例にとると、
for { i <- range(1, n)
j <- range(1, i)
if isPrime(i+j)
} yield {i, j}
#co(){
Here is what we get when we translate this expression:
}
この式を翻訳すると次が得られます。
range(1, n)
.flatMap(i =>
range(1, i)
.filter(j => isPrime(i+j))
.map(j => (i, j)))
#co(){
Conversely, it would also be possible to express functions map, flatMap and filter using for-comprehensions. Here are the three functions again, this time implemented using for-comprehensions.
}
逆に、関数 map, flatMap, filter を for 内包表記で表すこともできます。3つの関数を今度は for 内包表記を使って実装してみます。
object Demo {
def map[A, B](xs: List[A], f: A => B): List[B] =
for (x <- xs) yield f(x)
def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] =
for (x <- xs; y <- f(x)) yield y
def filter[A](xs: List[A], p: A => Boolean): List[A] =
for (x <- xs if p(x)) yield x
}
#co(){
Not surprisingly, the translation of the for-comprehension in the body of Demo.map will produce a call to map in class List. Similarly, Demo.flatMap and Demo.filter translate to flatMap and filter in class List.
}
驚くことではありませんが、Demo.map の本体の for 内包表記の翻訳は、クラス List の map を呼び出します。同様に、Demo.flatMap と Demo.filter は、クラス List の flatMap と filterの呼び出しへ翻訳されます。
#co(){
Exercise 10.3.1 Define the following function in terms of for.
}
&b(){演習 10.3.1 } 次の関数を for を用いて定義しなさい。
def flatten[A](xss: List[List[A]]): List[A] =
(xss :\ (Nil: List[A])) ((xs, ys) => xs ::: ys)
#co(){
Exercise 10.3.2 Translate
}
&b(){演習 10.3.2 } 次を高階関数を用いて翻訳しなさい。
for (b <- books; a <- b.authors if a startsWith "Bird") yield b.title
for (b <- books if (b.title indexOf "Program") >= 0) yield b.title
#co(){
to higher-order functions.
}
#center(){[[前ページ>Example10.2]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.4]]}
----
#comment