Example10.2

「Example10.2」の編集履歴(バックアップ)一覧はこちら

Example10.2」(2011/02/24 (木) 08:49:46) の最新版変更点

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

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

#co(){ 10.2 Querying with For-Comprehensions The for-notation is essentially equivalent to common operations of database query languages. For instance, say we are given a database books, represented as a list of books, where Book is defined as follows. } ** 10.2 For 内包表記によるクエリ for 内包表記は、データベースのクエリ言語による一般的操作と本質的に同じです。たとえば、データベース books があり、本のリストとして表現されているとしましょう。ただし Book は次のように定義されるものとします。 case class Book(title: String, authors: List[String]) #co(){ Here is a small example database: } 次はデータベースの小さなサンプルです。 val books: List[Book] = List( Book("Structure and Interpretation of Computer Programs", List("Abelson, Harold", "Sussman, Gerald J.")), Book("Principles of Compiler Design", List("Aho, Alfred", "Ullman, Jeffrey")), Book("Programming in Modula-2", List("Wirth, Niklaus")), Book("Introduction to Functional Programming"), List("Bird, Richard")), Book("The Java Language Specification", List("Gosling, James", "Joy, Bill", "Steele, Guy", "Bracha, Gilad"))) #co(){ Then, to find the titles of all books whose author's last name is "Ullman": } このとき、著者の姓が "Ullman" である本すべての書名を探すには、 for (b <- books; a <- b.authors if a startsWith "Ullman") yield b.title #co(){ (Here, startsWith is a method in java.lang.String). Or, to find the titles of all books that have the string "Program" in their title: } (ここで startsWith は java.lang.String のメソッド)。あるいは書名が "Program" で始まる本の書名を探すには for (b <- books if (b.title indexOf "Program") >= 0) yield b.title #co(){ Or, to find the names of all authors that have written at least two books in the database. } あるいは、少なくとも二冊の本を書いたすべての著者の名前をデータベース上で探すには、 for (b1 <- books; b2 <- books if b1 != b2; a1 <- b1.authors; a2 <- b2.authors if a1 == a2) yield a1 #co(){ The last solution is not yet perfect, because authors will appear several times in the list of results. We still need to remove duplicate authors from result lists. This can be achieved with the following function. } 最後のコードはまだ完全ではありません。なぜなら著者が結果リストに複数回現れるからです。結果リストから重複した著者を除く必要があります。これは次の関数を使えばできます。 def removeDuplicates[A](xs: List[A]): List[A] = if (xs.isEmpty) xs else xs.head :: removeDuplicates(xs.tail filter (x => x != xs.head)) #co(){ Note that the last expression in method removeDuplicates can be equivalently expressed using a for-comprehension. } メソッド removeDuplicates の最後の式は for 内包表記を使って、等価に次のように表現できます。 xs.head :: removeDuplicates(for (x <- xs.tail if x != xs.head) yield x) #center(){[[前ページ>Example10.1]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.3]]} ---- #comment
#co(){ 10.2 Querying with For-Comprehensions The for-notation is essentially equivalent to common operations of database query languages. For instance, say we are given a database books, represented as a list of books, where Book is defined as follows. } #setmenu2(ex-r-menu) ** 10.2 For 内包表記によるクエリ (Querying with For-Comprehensions) for 内包表記は、データベースのクエリ言語による一般的操作と本質的に同じです。たとえば、データベース books があり、本のリストとして表現されているとしましょう。ただし Book は次のように定義されるものとします。 case class Book(title: String, authors: List[String]) #co(){ Here is a small example database: } 次はデータベースの小さなサンプルです。 val books: List[Book] = List( Book("Structure and Interpretation of Computer Programs", List("Abelson, Harold", "Sussman, Gerald J.")), Book("Principles of Compiler Design", List("Aho, Alfred", "Ullman, Jeffrey")), Book("Programming in Modula-2", List("Wirth, Niklaus")), Book("Introduction to Functional Programming"), List("Bird, Richard")), Book("The Java Language Specification", List("Gosling, James", "Joy, Bill", "Steele, Guy", "Bracha, Gilad"))) #co(){ Then, to find the titles of all books whose author's last name is "Ullman": } このとき、著者の姓が "Ullman" である本すべての書名を探すには、 for (b <- books; a <- b.authors if a startsWith "Ullman") yield b.title #co(){ (Here, startsWith is a method in java.lang.String). Or, to find the titles of all books that have the string "Program" in their title: } (ここで startsWith は java.lang.String のメソッド)。あるいは書名が "Program" で始まる本の書名を探すには for (b <- books if (b.title indexOf "Program") >= 0) yield b.title #co(){ Or, to find the names of all authors that have written at least two books in the database. } あるいは、少なくとも二冊の本を書いたすべての著者の名前をデータベース上で探すには、 for (b1 <- books; b2 <- books if b1 != b2; a1 <- b1.authors; a2 <- b2.authors if a1 == a2) yield a1 #co(){ The last solution is not yet perfect, because authors will appear several times in the list of results. We still need to remove duplicate authors from result lists. This can be achieved with the following function. } 最後のコードはまだ完全ではありません。なぜなら著者が結果リストに複数回現れるからです。結果リストから重複した著者を除く必要があります。これは次の関数を使えばできます。 def removeDuplicates[A](xs: List[A]): List[A] = if (xs.isEmpty) xs else xs.head :: removeDuplicates(xs.tail filter (x => x != xs.head)) #co(){ Note that the last expression in method removeDuplicates can be equivalently expressed using a for-comprehension. } メソッド removeDuplicates の最後の式は for 内包表記を使って、等価に次のように表現できます。 xs.head :: removeDuplicates(for (x <- xs.tail if x != xs.head) yield x) #center(){[[前ページ>Example10.1]] [[ 10 章>Chapter 10 For-Comprehensions]] [[目次>ScalaByExample和訳]] [[次ページ>Example10.3]]} ---- #comment

表示オプション

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

下から選んでください:

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