「Example13.1」の編集履歴(バックアップ)一覧はこちら
Example13.1 - (2011/02/24 (木) 08:59:41) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
#co(){
13.1 Iterator Methods
Iterators support a rich set of methods besides next and hasNext, which is described in the following. Many of these methods mimic a corresponding functionality in lists.
}
** 13.1 イテレータメソッド (Iterator Method)
イテレータは、next と hasNext のほかにも豊富なメソッドをサポートしており、それを次で説明します。それらメソッドの多くは、リスト機能の対応するものに似ています。
#co(){
Append. Method append constructs an iterator which resumes with the given iterator it after the current iterator has finished.
}
&b(){Append } メソッド append は、新しいイテレータを構築します。構築されるイテレータは、元のイテレータの最後まで到達すると、与えられたイテレータで継続します。
def append[B >: A](that: Iterator[B]): Iterator[B] = new Iterator[B] {
def hasNext = Iterator.this.hasNext || that.hasNext
def next = if (Iterator.this.hasNext) Iterator.this.next else that.next
}
#co(){
The terms Iterator.this.next and Iterator.this.hasNext in the definition of append call the corresponding methods as they are defined in the enclosing Iterator class. If the Iterator prefix to this would have been missing, hasNext and next would have called recursively the methods being defined in the result of append, which is not what we want.
}
append 定義にある Iterator.this.next という項と Iterator.this.hasNext という項は、それを取り囲む Iterator クラスで定義されている、対応するメソッドを呼び出します。もし、this に対するプレフィックス Iterator がなければ、hasNext と next は、append の結果(オブジェクト)に定義されているメソッド自身を再帰的に呼び出してしまいます。これは我々の望むことではありません。
#co(){
Map, FlatMap, Foreach. Method map constructs an iterator which returns all elements of the original iterator transformed by a given function f.
}
&b(){Map、FlatMap、Foreach } メソッド map は、元のイテレータのすべての要素を、与えられた関数 f で変換して返すイテレータを構築します。
def map[B](f: A => B): Iterator[B] = new Iterator[B] {
def hasNext = Iterator.this.hasNext
def next = f(Iterator.this.next)
}
#co(){
Method flatMap is like method map, except that the transformation function f now returns an iterator. The result of flatMap is the iterator resulting from appending together all iterators returned from successive calls of f.
}
メソッド flatMap はメソッド map と似ていますが、変換する関数 f がイテレータを返す点が違います。flatmap の結果は、f を順に呼んでいって返されてくるイテレータ達を結合したものです。
def flatMap[B](f: A => Iterator[B]): Iterator[B] = new Iterator[B] {
private var cur: Iterator[B] = Iterator.empty
def hasNext: Boolean =
if (cur.hasNext) true
else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); hasNext }
else false
def next: B =
if (cur.hasNext) cur.next
else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); next }
else error("next on empty iterator")
}
#co(){
Closely related to map is the foreach method, which applies a given function to all elements of an iterator, but does not construct a list of results
}
map に深く関係するのが foreach メソッドです。与えられた関数をイテレータのすべての要素に適用しますが、結果のリストを構築しません。
def foreach(f: A => Unit): Unit =
while (hasNext) { f(next) }
#co(){
Filter. Method filter constructs an iterator which returns all elements of the original iterator that satisfy a criterion p.
}
&b(){Filter } メソッド filter は、元のイテレータのすべての要素のうち、基準 p を満たすものを返すイテレータを構築します。
def filter(p: A => Boolean) = new BufferedIterator[a] {
private val source =
Iterator.this.buffered
private def skip
{ while (source.hasNext && !p(source.head)) { source.next } }
def hasNext: Boolean =
{ skip; source.hasNext }
def next: A =
{ skip; source.next }
def head: A =
{ skip; source.head }
}
#co(){
In fact, filter returns instances of a subclass of iterators which are "buffered". A BufferedIterator object is an iterator which has in addition a method head. This method returns the element which would otherwise have been returned by head, but does not advance beyond that element. Hence, the element returned by head is returned again by the next call to head or next. Here is the definition of the BufferedIterator trait.
}
filter はイテレータの「バッファされた」サブクラスのインスタンスを返します。BufferedIterator オブジェクトは、そのほかに head メソッドを持っています。このメソッドは、head (訳注 : next の誤り?) が返すはずの要素を返しますが、next のように、要素をそれ以降に進めることはありません。そのため、head が返す要素は、次の head または next の呼び出しで再び返されます。BufferedIterator トレイトの定義は次のとおりです。
trait BufferedIterator[+A] extends Iterator[A] {
def head: A
}
#co(){
Since map, flatMap, filter, and foreach exist for iterators, it follows that for comprehensions and for-loops can also be used on iterators. For instance, the application which prints the squares of numbers between 1 and 100 could have equivalently been expressed as follows.
}
map、flatmap、filter、foreach がイテレータに存在するので、for 内包表記 と for ループ がイテレータに対しても使えます。たとえば、1 から 100 までの数の平方を表示する適用は、次のように等価に表現できます。
for (i <Iterator.range(1, 100))
println(i * i)
#co(){
Zip. Method zip takes another iterator and returns an iterator consisting of pairs of corresponding elements returned by the two iterators.
}
&b(){Zip } メソッド zip は、他のイテレータをとって、2つのイテレータから返される要素のペアからなるイテレータを返します。
def zip[B](that: Iterator[B]) = new Iterator[(A, B)] {
def hasNext = Iterator.this.hasNext && that.hasNext
def next = {Iterator.this.next, that.next}
}
}
#center(){[[前ページ>Chapter 13 Iterators]] [[ 13 章>Chapter 13 Iterators]] [[目次>ScalaByExample和訳]] [[次ページ>Example13.2]]}
----
#comment
#co(){
13.1 Iterator Methods
Iterators support a rich set of methods besides next and hasNext, which is described in the following. Many of these methods mimic a corresponding functionality in lists.
}
#setmenu2(ex-r-menu)
** 13.1 イテレータメソッド (Iterator Method)
イテレータは、next と hasNext のほかにも豊富なメソッドをサポートしており、それを次で説明します。それらメソッドの多くは、リスト機能の対応するものに似ています。
#co(){
Append. Method append constructs an iterator which resumes with the given iterator it after the current iterator has finished.
}
&b(){Append } メソッド append は、新しいイテレータを構築します。構築されるイテレータは、元のイテレータの最後まで到達すると、与えられたイテレータで継続します。
def append[B >: A](that: Iterator[B]): Iterator[B] = new Iterator[B] {
def hasNext = Iterator.this.hasNext || that.hasNext
def next = if (Iterator.this.hasNext) Iterator.this.next else that.next
}
#co(){
The terms Iterator.this.next and Iterator.this.hasNext in the definition of append call the corresponding methods as they are defined in the enclosing Iterator class. If the Iterator prefix to this would have been missing, hasNext and next would have called recursively the methods being defined in the result of append, which is not what we want.
}
append 定義にある Iterator.this.next という項と Iterator.this.hasNext という項は、それを取り囲む Iterator クラスで定義されている、対応するメソッドを呼び出します。もし、this に対するプレフィックス Iterator がなければ、hasNext と next は、append の結果(オブジェクト)に定義されているメソッド自身を再帰的に呼び出してしまいます。これは我々の望むことではありません。
#co(){
Map, FlatMap, Foreach. Method map constructs an iterator which returns all elements of the original iterator transformed by a given function f.
}
&b(){Map、FlatMap、Foreach } メソッド map は、元のイテレータのすべての要素を、与えられた関数 f で変換して返すイテレータを構築します。
def map[B](f: A => B): Iterator[B] = new Iterator[B] {
def hasNext = Iterator.this.hasNext
def next = f(Iterator.this.next)
}
#co(){
Method flatMap is like method map, except that the transformation function f now returns an iterator. The result of flatMap is the iterator resulting from appending together all iterators returned from successive calls of f.
}
メソッド flatMap はメソッド map と似ていますが、変換する関数 f がイテレータを返す点が違います。flatmap の結果は、f を順に呼んでいって返されてくるイテレータ達を結合したものです。
def flatMap[B](f: A => Iterator[B]): Iterator[B] = new Iterator[B] {
private var cur: Iterator[B] = Iterator.empty
def hasNext: Boolean =
if (cur.hasNext) true
else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); hasNext }
else false
def next: B =
if (cur.hasNext) cur.next
else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); next }
else error("next on empty iterator")
}
#co(){
Closely related to map is the foreach method, which applies a given function to all elements of an iterator, but does not construct a list of results
}
map に深く関係するのが foreach メソッドです。与えられた関数をイテレータのすべての要素に適用しますが、結果のリストを構築しません。
def foreach(f: A => Unit): Unit =
while (hasNext) { f(next) }
#co(){
Filter. Method filter constructs an iterator which returns all elements of the original iterator that satisfy a criterion p.
}
&b(){Filter } メソッド filter は、元のイテレータのすべての要素のうち、基準 p を満たすものを返すイテレータを構築します。
def filter(p: A => Boolean) = new BufferedIterator[a] {
private val source =
Iterator.this.buffered
private def skip
{ while (source.hasNext && !p(source.head)) { source.next } }
def hasNext: Boolean =
{ skip; source.hasNext }
def next: A =
{ skip; source.next }
def head: A =
{ skip; source.head }
}
#co(){
In fact, filter returns instances of a subclass of iterators which are "buffered". A BufferedIterator object is an iterator which has in addition a method head. This method returns the element which would otherwise have been returned by head, but does not advance beyond that element. Hence, the element returned by head is returned again by the next call to head or next. Here is the definition of the BufferedIterator trait.
}
filter はイテレータの「バッファされた」サブクラスのインスタンスを返します。BufferedIterator オブジェクトは、そのほかに head メソッドを持っています。このメソッドは、head (訳注 : next の誤り?) が返すはずの要素を返しますが、next のように、要素をそれ以降に進めることはありません。そのため、head が返す要素は、次の head または next の呼び出しで再び返されます。BufferedIterator トレイトの定義は次のとおりです。
trait BufferedIterator[+A] extends Iterator[A] {
def head: A
}
#co(){
Since map, flatMap, filter, and foreach exist for iterators, it follows that for comprehensions and for-loops can also be used on iterators. For instance, the application which prints the squares of numbers between 1 and 100 could have equivalently been expressed as follows.
}
map、flatmap、filter、foreach がイテレータに存在するので、for 内包表記 と for ループ がイテレータに対しても使えます。たとえば、1 から 100 までの数の平方を表示する適用は、次のように等価に表現できます。
for (i <Iterator.range(1, 100))
println(i * i)
#co(){
Zip. Method zip takes another iterator and returns an iterator consisting of pairs of corresponding elements returned by the two iterators.
}
&b(){Zip } メソッド zip は、他のイテレータをとって、2つのイテレータから返される要素のペアからなるイテレータを返します。
def zip[B](that: Iterator[B]) = new Iterator[(A, B)] {
def hasNext = Iterator.this.hasNext && that.hasNext
def next = {Iterator.this.next, that.next}
}
}
#center(){[[前ページ>Chapter 13 Iterators]] [[ 13 章>Chapter 13 Iterators]] [[目次>ScalaByExample和訳]] [[次ページ>Example13.2]]}
----
#comment