This repository was archived by the owner on Mar 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollections_44.html
More file actions
138 lines (138 loc) · 6.01 KB
/
collections_44.html
File metadata and controls
138 lines (138 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" />
<title>Scala 2.8 コレクション API -- バッファ付きイテレータ</title>
<link rel="stylesheet" type="text/css" href="guide.css" />
</head>
<body dir="ltr">
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><img alt="" src="blank.png" /></td>
<td bgcolor="#99CCFF"><a href="collections_43.html"><img border="0"
alt="イテレータ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><img alt="" src="blank.png" /></td>
<td align="center" bgcolor="#99CCFF" width="100%">
<b>バッファ付きイテレータ</b></td>
<td bgcolor="#99CCFF" align="center" class="tocref"><a href=
"collections_49.html">目次</a></td>
</tr>
</table>
<blockquote style=
"border-left: 1px solid gray; font-family: Century, Times, 'Times New Roman', 'MS Gothic', serif; padding-left: 1em;">
最新版は <a href="http://docs.scala-lang.org/ja/overviews/collections/iterators.html">Scala Documentation</a> に移行しました。
</blockquote>
<h2>バッファ付きイテレータ</h2>
<p>
イテレータを前進させずに次に返る要素を検査できるような「先読み」できるイテレータが必要になることがたまにある。例えば、一連の文字列を返すイテレータがあるとして、その最初の空白文字列を飛ばすという作業を考える。以下のように書こうと思うかもしれない。</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt><font color=
"#0000E5">def</font> skipEmptyWordsNOT(it: <font color=
"#660099">Iterator[String]</font>) =</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt> <font color=
"#0000E5">while</font> (it.next().isEmpty) {}</tt></td>
</tr>
</table>
</div>
<p>
しかし、このコードを慎重に見ると間違っていることが分かるはずだ。コードは確かに先頭の空白文字列の続きを読み飛ばすが、<tt>it</tt>
は最初の非空白文字列も追い越してしまっているのだ。</p>
<p>この問題はバッファ付きイテレータを使うことで解決できる。<a href=
"http://www.scala-lang.org/api/current/scala/collection/BufferedIterator.html"><tt>BufferedIterator</tt></a>
トレイトは、<tt>head</tt> というメソッドを追加で提供する <a href=
"http://www.scala-lang.org/api/current/scala/collection/Iterator.html">
<tt>Iterator</tt></a> の子トレイトだ。バッファ付きイテレータに対して <tt>head</tt>
を呼び出すことで、イテレータを前進させずに最初の要素を返すことができる。バッファ付きイテレータを使うと、空白文字列を読み飛ばすのは以下のように書ける。</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt><font color=
"#0000E5">def</font> skipEmptyWords(it: <font color=
"#660099">BufferedIterator[String]</font>) =</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt> <font color=
"#0000E5">while</font> (it.head.isEmpty) { it.next() }</tt></td>
</tr>
</table>
</div>
<p><tt>buffered</tt>
メソッドを呼ぶことで全てのイテレータはバッファ付きイテレータに変換できる。次に具体例で説明する。</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> it = <font color=
"#660099">Iterator</font>(<font color=
"#000000">1</font>, <font color=
"#000000">2</font>, <font color=
"#000000">3</font>, <font color="#000000">4</font>)</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">it: Iterator[Int] = non-empty iterator</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> bit = it.buffered</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">bit: java.lang.</font></tt><tt><font color=
"#590000">Object with scala.collection.</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt> <font color=
"#660099">BufferedIterator[Int]</font> = non-empty iterator</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> bit.head</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res10: Int = 1</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> bit.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res11: Int = 1</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> bit.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res11: Int = 2</font></tt></td>
</tr>
</table>
</div>
<p>バッファ付きイテレータに対して <tt>head</tt> を呼び出してもイテレータ <tt>bit</tt>
は前進しないことに注意してほしい。よって、後続の <tt>bit.next()</tt> の呼び出しは
<tt>bit.head</tt> と同じ値を返す。</p>
<p>続いては、<a href="collections_45.html">コレクションの作成</a></p>
<hr />
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><img alt="" src="blank.png" /></td>
<td bgcolor="#99CCFF"><a href="collections_43.html"><img border="0"
alt="イテレータ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><img alt="" src="blank.png" /></td>
<td align="center" bgcolor="#99CCFF" width="100%">
<b>バッファ付きイテレータ</b></td>
<td bgcolor="#99CCFF" align="center" class="tocref"><a href=
"collections_49.html">目次</a></td>
</tr>
</table>
</body>
</html>