没有合适的资源?快使用搜索试试~ 我知道了~
Scala By Examples
需积分: 3 57 下载量 39 浏览量
2009-05-17
09:58:39
上传
评论 1
收藏 867KB PDF 举报
温馨提示
试读
145页
学习一门程序设计语言,如果光看语法而不动手实践,是无法掌握的。作为一个初学者,往往苦于找不到合适的例子进行练级。《Programming in Scala》是一本较好的Scala入门教材,但是书中的例子过于分散。鉴于此,《programming in Scala》的作者别出心裁,针对一些主题为我们提供了丰富的例子。
资源推荐
资源详情
资源评论
Scala By Example
DRAFT
April 15, 2009
Martin Odersky
PROGRAMMING METHODS LABORATORY
EPFL
SWITZERLAND
Contents
1 Introduction 1
2 A First Example 3
3 Programming with Actors and Messages 7
4 Expressions and Simple Functions 11
4.1 Expressions And Simple Functions . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.3 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 Example: Square Roots by Newton’s Method . . . . . . . . . . . . . . . . 15
4.5 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.6 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 First-Class Functions 21
5.1 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.2 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.3 Example: Finding Fixed Points of Functions . . . . . . . . . . . . . . . . 25
5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.5 Language Elements Seen So Far . . . . . . . . . . . . . . . . . . . . . . . 28
6 Classes and Objects 31
7 Case Classes and Pattern Matching 43
7.1 Case Classes and Case Objects . . . . . . . . . . . . . . . . . . . . . . . . 46
7.2 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8 Generic Types and Methods 51
8.1 Type Parameter Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
8.2 Variance Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
iv CONTENTS
8.3 Lower Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
8.4 Least Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
8.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9 Lists 63
9.1 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
9.2 Definition of class List I: First Order Methods . . . . . . . . . . . . . . . 65
9.3 Example: Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.4 Definition of class List II: Higher-Order Methods . . . . . . . . . . . . . 70
9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10 For-Comprehensions 79
10.1 The N-Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
10.2 Querying with For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 81
10.3 Translation of For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 82
10.4 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
10.5 Generalizing For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11 Mutable State 87
11.1 Stateful Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
11.2 Imperative Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . 91
11.3 Extended Example: Discrete Event Simulation . . . . . . . . . . . . . . . 92
11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
12 Computing with Streams 99
13 Iterators 103
13.1 Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
13.2 Constructing Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
13.3 Using Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14 Lazy Values 109
15 Implicit Parameters and Conversions 113
CONTENTS v
16 Hindley/Milner Type Inference 117
17 Abstractions for Concurrency 125
17.1 Signals and Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
17.2 SyncVars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
17.3 Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
17.4 Parallel Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
17.5 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
17.6 Readers/Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
17.7 Asynchronous Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
17.8 Synchronous Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
17.9 Workers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
17.10Mailboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
17.11Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
剩余144页未读,继续阅读
资源评论
ggd5434phvjs
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功