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_14.html
More file actions
119 lines (119 loc) · 5.95 KB
/
collections_14.html
File metadata and controls
119 lines (119 loc) · 5.95 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
<!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"><a href="collections_15.html"><img border="0"
alt="ベクトル" src="next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_12.html"><img border="0"
alt="具象不変コレクションクラス" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_13.html"><img border="0"
alt="リスト" src="previous.png" /></a></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/concrete-immutable-collection-classes.html">Scala Documentation</a> に移行しました。
</blockquote>
<h2>ストリーム</h2>
<p>ストリーム (<a href=
"http://www.scala-lang.org/api/current/scala/collection/immutable/Stream.html"><tt>Stream</tt></a>)
はリストに似ているが、要素は遅延評価される。そのため、ストリームは無限の長さをもつことができる。呼び出された要素のみが計算される。他の点においては、ストリームはリストと同じ性能特性をもつ。</p>
<p>リストは <tt>::</tt> 演算子によって構築されるが、ストリームはそれに似た <tt>#::</tt>
演算子によって構築される。以下は、整数の 1, 2, 3 からなる簡単なストリームの例だ:</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> str = <font color=
"#000000">1</font> #:: <font color=
"#000000">2</font> #:: <font color=
"#000000">3</font> #:: Stream.empty</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">str: scala.collection.immutable.</font></tt><tt><font color="#590000">Stream[Int] = Stream(1, ?)</font></tt></td>
</tr>
</table>
</div>
<p>このストリームの head は 1 で、tail は 2 と 3 だ。上の例では tail
が表示されていないが、それはまだ計算されていないからだ。ストリームは遅延評価されるため、<tt>toString</tt>
は余計な評価を強いないように慎重に設計されているのだ。</p>
<p>
以下に、もう少し複雑な例を示す。任意の二つの数から始まるフィボナッチ数列を計算するストリームだ。フィボナッチ数列とは、それぞれの要素がその前二つの要素の和である数列のことだ。</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left">
<tt> scala> <font color=
"#0000E5">def</font> fibFrom(a: <font color=
"#660099">Int</font>, b: <font color=
"#660099">Int</font>): <font color=
"#660099">Stream[Int]</font> = a #:: fibFrom(b, </tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>a + b)</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000"> fibFrom: (a: Int,b: Int)Stream[Int]</font></tt></td>
</tr>
</table>
</div>
<p>この関数は嘘のように単純だ。数列の最初の要素は明らかに <tt>a</tt> で、残りは <tt>b</tt> そして
<tt>a</tt> <tt>+</tt> <tt>b</tt>
から始まるフィボナッチ数列だ。無限の再帰呼び出しに陥らずにこの数列を計算するのが難しい所だ。もしこの関数が <tt>#::</tt>
の代わりに <tt>::</tt>
を使っていたなら、全ての呼び出しはまた別の呼び出しを招くため、無限の再帰呼び出しに陥ってしまう。しかし、<tt>#::</tt>
を使っているため、右辺は呼び出されるまでは評価されないのだ。</p>
<p>二つの 1 から始まるフィボナッチ数列の最初の数要素を以下に示す:</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> fibs = fibFrom(<font color=
"#000000">1</font>, <font color=
"#000000">1</font>).take(<font color="#000000">7</font>)</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">fibs: scala.collection.immutable.</font></tt><tt><font color="#590000">Stream[Int] = Stream(1, ?)</font></tt></td>
</tr>
<tr>
<td colspan="99" align="left">
<tt>scala> fibs.toList</tt></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res9: List[Int] = List(1, 1, 2, 3, 5, 8, 11)</font></tt></td>
</tr>
</table>
</div>
<p>続いては、<a href="collections_15.html">ベクトル</a></p>
<hr />
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><a href="collections_15.html"><img border="0"
alt="ベクトル" src="next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_12.html"><img border="0"
alt="具象不変コレクションクラス" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_13.html"><img border="0"
alt="リスト" src="previous.png" /></a></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>