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_4.html
More file actions
258 lines (258 loc) · 11.2 KB
/
collections_4.html
File metadata and controls
258 lines (258 loc) · 11.2 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!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 - Iterable トレイト</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"><a href="collections_5.html"><img border="0"
alt="列トレイト Seq、IndexedSeq、および LinearSeq" src=
"next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_0.html"><img border="0"
alt="トップ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_3.html"><img border="0"
alt="Traversable トレイト" src="previous.png" /></a></td>
<td align="center" bgcolor="#99CCFF" width="100%"><b><tt>Iterable</tt>
トレイト</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/trait-iterable.html">Scala Documentation</a> に移行しました。
</blockquote>
<h1><tt>Iterable</tt> トレイト</h1>
<p>反復可能(<a href=
"http://www.scala-lang.org/api/current/scala/collection/Iterable.html"><tt>Iterable</tt></a>)トレイトは<a href="collections_2.html#id1">コレクション階層</a>の上から2番目に位置する。
このトレイトの全メソッドは、コレクション内の要素を一つづつ返す抽象メソッド <tt>iterator</tt>
に基づいている。<tt>Iterable</tt> では、<tt>Traversable</tt> トレイトの
<tt>foreach</tt> メソッドも <tt>iterator</tt>
に基づいて実装されている。以下が実際の実装だ:</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt><font color=
"#0000E5">def</font> foreach[U](f: <font color=
"#660099">Elem</font> => U): <font color=
"#660099">Unit</font> = {</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt> <font color=
"#0000E5">val</font> it = iterator</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt> <font color=
"#0000E5">while</font> (it.hasNext) f(it.next())</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>} </tt></td>
</tr>
</table>
</div>
<p>多くの <tt>Iterable</tt> のサブクラスは、より効率的な実装を提供するため、上の
<tt>foreach</tt> の標準実装をオーバーライドしている。<tt>foreach</tt> は
<tt>Traversable</tt> の全ての演算の基となっているため、効率的であることが重要なのだ。</p>
<p><tt>Iterable</tt> にはイテレータを返すもう二つのメソッドがある: <tt>grouped</tt> と
<tt>sliding</tt>
だ。これらのイテレータは単一の要素を返すのではなく、元のコレクションの部分列を返す。これらのメソッドに渡された引数がこの部分列の最大サイズとなる。<tt>grouped</tt>
メソッドは要素を n個づつの「かたまり」にして返すのに対し、 <tt>sliding</tt> は
n個の要素から成る「窓」をスライドさせて返す。この二つのメソッドの違いは REPL でのやりとりを見れば明らかになるはずだ。</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> xs = <font color=
"#660099">List</font>(<font color=
"#000000">1</font>, <font color=
"#000000">2</font>, <font color=
"#000000">3</font>, <font color=
"#000000">4</font>, <font color="#000000">5</font>)</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">xs: List[Int] = List(1, 2, 3, 4, 5)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> git = xs grouped <font color="#000000">3</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">git: Iterator[List[Int]] = non-empty iterator</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> git.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color="#590000">res0:
List[Int] = List(2, 3, 4)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> git.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res4: List[Int] = List(4, 5)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> sit = xs sliding <font color="#000000">3</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">sit: Iterator[List[Int]] = non-empty iterator</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> sit.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color="#590000">res0:
List[Int] = List(2, 3, 4)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> sit.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color="#590000">res0:
List[Int] = List(2, 3, 4)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> sit.next()</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color="#590000">res0:
List[Int] = List(2, 3, 4)</font></tt></td>
</tr>
</table>
</div>
<p><tt>Iterable</tt> トレイトは、 <tt>Traversable</tt>
からのメソッドの他に、イテレータがあることで効率的に実装することができる他のメソッドを追加する。それらのメソッドを以下の表にまとめる。</p>
<table border="0" cellspacing="0" class="ops">
<tbody>
<tr>
<th colspan="2" align="center"><a href=
"http://www.scala-lang.org/api/current/scala/collection/Iterable.html">
<tt>Iterable</tt></a> トレイトの演算 <a name="id1" id="id1"> </a>
<a name="tab:iterableops" id="tab:iterableops"> </a></th>
</tr>
<tr>
<th align="left" width="250">使用例</th>
<th align="left">振る舞い</th>
</tr>
<tr>
<th class="opcat" align="left" colspan="2">抽象メソッド:</th>
</tr>
<tr>
<td align="left"><tt>xs.iterator</tt></td>
<td align="left"><tt>xs</tt>内の全ての要素を <tt>foreach</tt>
が歩くのと同じ順序で返すイテレータ。</td>
</tr>
<tr>
<th class="opcat" align="left" colspan="2">他のイテレータ:</th>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>grouped</tt> <tt>size</tt></td>
<td align="left">このコレクション内の要素を固定サイズの「かたまり」にして返すイテレータ。</td>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>sliding</tt> <tt>size</tt></td>
<td align="left">このコレクション内の要素を固定サイズの「窓」をスライドさせて返すイテレータ。</td>
</tr>
<tr>
<th class="opcat" align="left" colspan="2">サブコレクション取得演算:</th>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>takeRight</tt> <tt>n</tt></td>
<td align="left"><tt>xs</tt> の最後の <tt>n</tt>個の要素から成るコレクション
(順序が定義されていない場合は、任意の <tt>n</tt>個の要素から成るコレクション)。</td>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>dropRight</tt> <tt>n</tt></td>
<td align="left">コレクションから <tt>xs</tt> <tt>takeRight</tt> <tt>n</tt>
を除いた残りの部分。</td>
</tr>
<tr>
<th class="opcat" align="left" colspan="2">zip 演算:</th>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>zip</tt> <tt>ys</tt></td>
<td align="left"><tt>xs</tt> と <tt>ys</tt> のそれぞれから対応する要素をペアにした
<tt>Iterable</tt>。</td>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>zipAll</tt> <tt>(ys,</tt> <tt>x,</tt> <tt>y)</tt></td>
<td align="left"><tt>xs</tt> と <tt>ys</tt> のそれぞれから対応する要素をペアにした
<tt>Iterable</tt> で、もし片方が短い場合は <tt>x</tt> か <tt>y</tt> を使って長いほうに合わせる。</td>
</tr>
<tr>
<td align="left"><tt>xs.zipWithIndex</tt></td>
<td align="left"><tt>xs</tt>内の要素とその添字をペアにした <tt>Iterable</tt>。</td>
</tr>
<tr>
<th class="opcat" align="left" colspan="2">比較演算:</th>
</tr>
<tr>
<td align="left">
<tt>xs</tt> <tt>sameElements</tt> <tt>ys</tt></td>
<td align="left"><tt>xs</tt> と <tt>ys</tt>
が同じ要素を同じ順序で格納しているかを調べる。</td>
</tr>
</tbody>
</table>
<p>継承階層では <tt>Iterable</tt> 直下に <a href=
"collections_5.html"><tt>Seq</tt></a>、<a href=
"collections_7.html"><tt>Set</tt></a>、<a href=
"collections_10.html"><tt>Map</tt></a> と三つのトレイトがある
。この三つのトレイトに共通することは <tt>apply</tt> メソッドと <tt>isDefinedAt</tt>
メソッドを持ったトレイト <a href=
"http://www.scala-lang.org/api/current/scala/PartialFunction.html"><tt>
PartialFunction</tt></a> を実装しているということだ。しかし、<a href=
"http://www.scala-lang.org/api/current/scala/PartialFunction.html"><tt>PartialFunction</tt></a>
の実装方法は三者三様である。</p>
<p>列は <tt>apply</tt> を位置的な添字として用いられており、要素は常に <tt>0</tt>
から数えられる。だから、<tt>Seq(1,</tt> <tt>2,</tt> <tt>3)(1)</tt> は
<tt>2</tt> を返す。セットでは <tt>apply</tt>
は所属を調べるのに用いられる。例えば、<tt>Set('a',</tt> <tt>'b',</tt> <tt>'c')('b')</tt>
は <tt>true</tt> を返し、<tt>Set()('a')</tt> は <tt>false</tt> を返す。
最後に、マップでは <tt>apply</tt>
は要素の選択に用いられている。例えば、<tt>Map('a'</tt> <tt>-></tt> <tt>1,</tt> <tt>'b'</tt> <tt>-></tt> <tt>10,</tt> <tt>'c'</tt> <tt>-></tt> <tt>100)('b')</tt>
は <tt>10</tt> を返す。</p>
<p>次に、この三つのコレクションをより詳しく説明しよう。</p>
<p>続いては、<a href="collections_5.html">列トレイト
<tt>Seq</tt>、<tt>IndexedSeq</tt>、および <tt>LinearSeq</tt></a></p>
<hr />
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><a href="collections_5.html"><img border="0"
alt="列トレイト Seq、IndexedSeq、および LinearSeq" src=
"next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_0.html"><img border="0"
alt="トップ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_3.html"><img border="0"
alt="Traversable トレイト" src="previous.png" /></a></td>
<td align="center" bgcolor="#99CCFF" width="100%"><b><tt>Iterable</tt>
トレイト</b></td>
<td bgcolor="#99CCFF" align="center" class="tocref"><a href=
"collections_49.html">目次</a></td>
</tr>
</table>
</body>
</html>