没有合适的资源?快使用搜索试试~ 我知道了~
Programming in Scala
需积分: 9 19 下载量 87 浏览量
2009-11-30
16:59:28
上传
评论
收藏 1.94MB PDF 举报
温馨提示
试读
327页
Scala is a programming language that fuses elements from object-oriented and functional programming. This part introduces Scala in an informal way, through a sequence of examples.
资源推荐
资源详情
资源评论
Programming in Scala
DRAFT
June 13, 2007
Martin Odersky
PROGRAMMING METHODS LABORATORY
EPFL
SWITZERLAND
Contents
I Rationale 1
II Scala by Example 7
1 A First Example 11
2 Programming with Actors and Messages 15
3 Expressions and Simple Functions 19
3.1 Expressions And Simple Functions . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4 Example: Square Roots by Newton’s Method . . . . . . . . . . . . . . . . 23
3.5 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.6 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4 First-Class Functions 29
4.1 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.2 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.3 Example: Finding Fixed Points of Functions . . . . . . . . . . . . . . . . 34
4.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4.5 Language Elements Seen So Far . . . . . . . . . . . . . . . . . . . . . . . 36
5 Classes and Objects 39
6 Case Classes and Pattern Matching 51
6.1 Case Classes and Case Objects . . . . . . . . . . . . . . . . . . . . . . . . 54
6.2 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
iv CONTENTS
7 Generic Types and Methods 59
7.1 Type Parameter Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
7.2 Variance Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.3 Lower Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.4 Least Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8 Lists 71
8.1 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.2 Definition of class List I: First Order Methods . . . . . . . . . . . . . . . 73
8.3 Example: Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.4 Definition of class List II: Higher-Order Methods . . . . . . . . . . . . . 78
8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9 For-Comprehensions 87
9.1 The N-Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
9.2 Querying with For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 89
9.3 Translation of For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 90
9.4 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
9.5 Generalizing For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
10 Mu table State 95
10.1 Stateful Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
10.2 Imperative Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . 99
10.3 Extended Example: Discrete Event Simulation . . . . . . . . . . . . . . . 100
10.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11 Computing with Streams 107
12 Iterators 111
12.1 Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
12.2 Constructing Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
12.3 Using Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
CONTENTS v
13 Implicit Parameters and Conversions 117
14 Combinator Parsing 121
14.1 Simple Combinator Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . 121
14.2 Parsers that Produce Results . . . . . . . . . . . . . . . . . . . . . . . . . 125
15 Hindley/Milner Type Inference 131
16 Abstractions for Concurrency 141
16.1 Signals and Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
16.2 SyncVars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
16.3 Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
16.4 Parallel Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
16.5 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
16.6 Readers/Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
16.7 Asynchronous Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
16.8 Synchronous Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
16.9 Workers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
16.10Mailboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
16.11Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
III The Scala Language Specification
Version 1.0 155
17 Lexical Syntax 159
17.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
17.2 Newline Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
17.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
17.3.1 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
17.3.2 Floating Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . 165
17.3.3 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
17.3.4 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
17.3.5 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
17.3.6 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
剩余326页未读,继续阅读
资源评论
jamesplus
- 粉丝: 0
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功