Tải bản đầy đủ (.pdf) (883 trang)

Artima programming in scala 2nd

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.77 MB, 883 trang )

A comprehensive step-by-step guide

Programming in

Scala
Second Edition

Updated for Scala 2.8

artima

Martin Odersky
Lex Spoon
Bill Venners

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Praise for the first edition of
Programming in Scala
Programming in Scala is probably one of the best programming books
I’ve ever read. I like the writing style, the brevity, and the thorough explanations. The book seems to answer every question as it enters my mind—it’s
always one step ahead of me. The authors don’t just give you some code
and take things for granted. They give you the meat so you really understand
what’s going on. I really like that.
- Ken Egervari, Chief Software Architect
Programming in Scala is clearly written, thorough, and easy to follow.
It has great examples and useful tips throughout. It has enabled our organization to ramp up on the Scala language quickly and efficiently. This book
is great for any programmer who is trying to wrap their head around the
flexibility and elegance of the Scala language.
- Larry Morroni, Owner, Morroni Technologies, Inc.


The Programming in Scala book serves as an excellent tutorial to the
Scala language. Working through the book, it flows well with each chapter
building on concepts and examples described in earlier ones. The book takes
care to explain the language constructs in depth, often providing examples
of how the language differs from Java. As well as the main language, there
is also some coverage of libraries such as containers and actors.
I have found the book really easy to work through, and it is probably
one of the better written technical books I have read recently. I really would
recommend this book to any programmer wanting to find out more about the
Scala language.
- Matthew Todd
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


iii

I am amazed by the effort undertaken by the authors of Programming in
Scala. This book is an invaluable guide to what I like to call Scala the Platform: a vehicle to better coding, a constant inspiration for scalable software
design and implementation. If only I had Scala in its present mature state
and this book on my desk back in 2003, when co-designing and implementing parts of the Athens 2004 Olympic Games Portal infrastructure!
To all readers: No matter what your programming background is, I feel
you will find programming in Scala liberating and this book will be a loyal
friend in the journey.
- Christos KK Loverdos, Software Consultant, Researcher
Programming in Scala is a superb in-depth introduction to Scala, and it’s
also an excellent reference. I’d say that it occupies a prominent place on my
bookshelf, except that I’m still carrying it around with me nearly everywhere
I go.
- Brian Clapper, President, ArdenTex, Inc.
Great book, well written with thoughtful examples. I would recommend

it to both seasoned programmers and newbies.
- Howard Lovatt
The book Programming in Scala is not only about how, but more importantly, why to develop programs in this new programming language. The
book’s pragmatic approach in introducing the power of combining objectoriented and functional programming leaves the reader without any doubts
as to what Scala really is.
- Dr. Ervin Varga, CEO/founder, EXPRO I.T. Consulting
This is a great introduction to functional programming for OO programmers. Learning about FP was my main goal, but I also got acquainted with
some nice Scala surprises like case classes and pattern matching. Scala is an
intriguing language and this book covers it well.
There’s always a fine line to walk in a language introduction book between giving too much or not enough information. I find Programming in
Scala to achieve a perfect balance.
- Jeff Heon, Programmer Analyst
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


iv
I bought an early electronic version of the Programming in Scala book,
by Odersky, Spoon, and Venners, and I was immediately a fan. In addition
to the fact that it contains the most comprehensive information about the
language, there are a few key features of the electronic format that impressed
me. I have never seen links used as well in a PDF, not just for bookmarks,
but also providing active links from the table of contents and index. I don’t
know why more authors don’t use this feature, because it’s really a joy for
the reader. Another feature which I was impressed with was links to the
forums (“Discuss”) and a way to send comments (“Suggest”) to the authors
via email. The comments feature by itself isn’t all that uncommon, but the
simple inclusion of a page number in what is generated to send to the authors
is valuable for both the authors and readers. I contributed more comments
than I would have if the process would have been more arduous.
Read Programming in Scala for the content, but if you’re reading the

electronic version, definitely take advantage of the digital features that the
authors took the care to build in!
- Dianne Marsh, Founder/Software Consultant, SRT Solutions
Lucidity and technical completeness are hallmarks of any well-written
book, and I congratulate Martin Odersky, Lex Spoon, and Bill Venners on a
job indeed very well done! The Programming in Scala book starts by setting
a strong foundation with the basic concepts and ramps up the user to an
intermediate level & beyond. This book is certainly a must buy for anyone
aspiring to learn Scala.
- Jagan Nambi, Enterprise Architecture, GMAC Financial Services
Programming in Scala is a pleasure to read. This is one of those wellwritten technical books that provide deep and comprehensive coverage of the
subject in an exceptionally concise and elegant manner.
The book is organized in a very natural and logical way. It is equally well
suited for a curious technologist who just wants to stay on top of the current
trends and a professional seeking deep understanding of the language core
features and its design rationales. I highly recommend it to all interested
in functional programming in general. For Scala developers, this book is
unconditionally a must-read.
- Igor Khlystov, Software Architect/Lead Programmer, Greystone Inc.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


v
The book Programming in Scala outright oozes the huge amount of hard
work that has gone into it. I’ve never read a tutorial-style book before that
accomplishes to be introductory yet comprehensive: in their (misguided) attempt to be approachable and not “confuse” the reader, most tutorials silently
ignore aspects of a subject that are too advanced for the current discussion.
This leaves a very bad taste, as one can never be sure as to the understanding
one has achieved. There is always some residual “magic” that hasn’t been

explained and cannot be judged at all by the reader. This book never does
that, it never takes anything for granted: every detail is either sufficiently
explained or a reference to a later explanation is given. Indeed, the text is
extensively cross-referenced and indexed, so that forming a complete picture
of a complex topic is relatively easy.
- Gerald Loeffler, Enterprise Java Architect
Programming in Scala by Martin Odersky, Lex Spoon, and Bill Venners:
in times where good programming books are rare, this excellent introduction
for intermediate programmers really stands out. You’ll find everything here
you need to learn this promising language.
- Christian Neukirchen

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Programming in Scala
Second Edition

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Programming in Scala
Second Edition
Martin Odersky, Lex Spoon, Bill Venners

artima
A RTIMA P RESS
WALNUT C REEK , C ALIFORNIA

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



viii
Programming in Scala
Second Edition
Martin Odersky is the creator of the Scala language and a professor at EPFL in
Lausanne, Switzerland. Lex Spoon worked on Scala for two years as a post-doc
with Martin Odersky. Bill Venners is president of Artima, Inc.
Artima Press is an imprint of Artima, Inc.
P.O. Box 305, Walnut Creek, California 94597
Copyright © 2007-2010 Martin Odersky, Lex Spoon, and Bill Venners.
All rights reserved.
First edition published as PrePrint™ eBook 2007
First edition published 2008
Second edition published as PrePrint™ eBook 2010
Second edition published 2010
Build date of this impression December 13, 2010
Produced in the United States of America
No part of this publication may be reproduced, modified, distributed, stored in a
retrieval system, republished, displayed, or performed, for commercial or
noncommercial purposes or for compensation of any kind without prior written
permission from Artima, Inc.
All information and materials in this book are provided "as is" and without
warranty of any kind.
The term “Artima” and the Artima logo are trademarks or registered trademarks of
Artima, Inc. All other company and/or product names may be trademarks or
registered trademarks of their owners.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



to Nastaran - M.O.
to Fay - L.S.
to Siew - B.V.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Overview
Contents
List of Figures
List of Tables
List of Listings
Foreword
Foreword to the First Edition
Acknowledgments
Introduction
1. A Scalable Language
2. First Steps in Scala
3. Next Steps in Scala
4. Classes and Objects
5. Basic Types and Operations
6. Functional Objects
7. Built-in Control Structures
8. Functions and Closures
9. Control Abstraction
10. Composition and Inheritance
11. Scala’s Hierarchy
12. Traits
13. Packages and Imports

14. Assertions and Unit Testing
15. Case Classes and Pattern Matching
16. Working with Lists
17. Collections
18. Stateful Objects
19. Type Parameterization
20. Abstract Members
21. Implicit Conversions and Parameters
22. Implementing Lists
23. For Expressions Revisited
24. The Scala Collections API
25. The Architecture of Scala Collections
26. Extractors
27. Annotations
28. Working with XML
29. Modular Programming Using Objects
30. Object Equality
31. Combining Scala and Java
32. Actors and Concurrency
33. Combinator Parsing
34. GUI Programming
35. The SCells Spreadsheet
A. Scala Scripts on Unix and Windows
Glossary
Bibliography
About the Authors
Index

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


xi
xxii
xxiv
xxvi
xxxiv
xxxvi
xxxviii
xli
49
68
81
103
117
139
159
184
207
222
250
258
277
295
309
344
377
399
422
447
479
503

516
532
607
631
647
655
669
684
710
724
759
788
800
825
826
842
845
846


Contents

Contents

xi

List of Figures

xxii


List of Tables

xxiv

List of Listings

xxvi

Foreword

xxxiv

Foreword to the First Edition

xxxvi

Acknowledgments

xxxviii

Introduction

xli

1

.
.
.
.

.

49
50
55
58
65
67

.
.
.
.
.

68
68
70
72
74
75

2

A Scalable Language
1.1 A language that grows on you
1.2 What makes Scala scalable? .
1.3 Why Scala? . . . . . . . . . .
1.4 Scala’s roots . . . . . . . . .
1.5 Conclusion . . . . . . . . . .


.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

First Steps in Scala
Step 1. Learn to use the Scala interpreter .
Step 2. Define some variables . . . . . .
Step 3. Define some functions . . . . . .
Step 4. Write some Scala scripts . . . . .
Step 5. Loop with while; decide with if

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents


xii

Step 6. Iterate with foreach and for . . . . . . . . . . . . .
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . .
3

4

5

6

77
80

Next Steps in Scala
Step 7. Parameterize arrays with types . . . .
Step 8. Use lists . . . . . . . . . . . . . . . .
Step 9. Use tuples . . . . . . . . . . . . . . .
Step 10. Use sets and maps . . . . . . . . . . .
Step 11. Learn to recognize the functional style
Step 12. Read lines from a file . . . . . . . . .
Conclusion . . . . . . . . . . . . . . . . . . .

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

81

81
85
90
91
96
99
102

Classes and Objects
4.1 Classes, fields, and methods
4.2 Semicolon inference . . . .
4.3 Singleton objects . . . . . .
4.4 A Scala application . . . .
4.5 The Application trait . . .
4.6 Conclusion . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

103
103
108
109
112
115
116

.
.
.
.
.
.
.
.
.
.


117
117
118
125
128
129
131
132
134
137
137

.
.
.
.
.

139
139
140
142
143
143

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


Basic Types and Operations
5.1 Some basic types . . . . . . . . . . .
5.2 Literals . . . . . . . . . . . . . . . .
5.3 Operators are methods . . . . . . . .
5.4 Arithmetic operations . . . . . . . .
5.5 Relational and logical operations . .
5.6 Bitwise operations . . . . . . . . . .
5.7 Object equality . . . . . . . . . . . .
5.8 Operator precedence and associativity
5.9 Rich wrappers . . . . . . . . . . . .
5.10 Conclusion . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


Functional Objects
6.1 A specification for class Rational . .
6.2 Constructing a Rational . . . . . . .
6.3 Reimplementing the toString method
6.4 Checking preconditions . . . . . . . .
6.5 Adding fields . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.


Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
7

8

9

Self references . . . . . .
Auxiliary constructors . .
Private fields and methods
Defining operators . . . .
Identifiers in Scala . . . .
Method overloading . . .
Implicit conversions . . .
A word of caution . . . .
Conclusion . . . . . . . .


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


xiii
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


145
146
148
149
151
154
156
157
157

Built-in Control Structures
7.1 If expressions . . . . . . . . . . . . . .
7.2 While loops . . . . . . . . . . . . . . .
7.3 For expressions . . . . . . . . . . . . . .
7.4 Exception handling with try expressions
7.5 Match expressions . . . . . . . . . . . .
7.6 Living without break and continue . .
7.7 Variable scope . . . . . . . . . . . . . .
7.8 Refactoring imperative-style code . . . .
7.9 Conclusion . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

159
160
161
164
169
173
175
177
181
183

.
.
.
.
.
.
.

.
.
.

184
184
186
188
190
191
192
195
199
202
206

.
.
.
.

207
207
211
213
215

Functions and Closures
8.1 Methods . . . . . . . . . . . .
8.2 Local functions . . . . . . . . .

8.3 First-class functions . . . . . .
8.4 Short forms of function literals
8.5 Placeholder syntax . . . . . . .
8.6 Partially applied functions . . .
8.7 Closures . . . . . . . . . . . .
8.8 Special function call forms . . .
8.9 Tail recursion . . . . . . . . . .
8.10 Conclusion . . . . . . . . . . .
Control Abstraction
9.1 Reducing code duplication . .
9.2 Simplifying client code . . .
9.3 Currying . . . . . . . . . . .
9.4 Writing new control structures

.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents
9.5
9.6

xiv

By-name parameters . . . . . . . . . . . . . . . . . . .
Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

10 Composition and Inheritance
10.1 A two-dimensional layout library . . . . . .
10.2 Abstract classes . . . . . . . . . . . . . . .
10.3 Defining parameterless methods . . . . . . .
10.4 Extending classes . . . . . . . . . . . . . .
10.5 Overriding methods and fields . . . . . . . .
10.6 Defining parametric fields . . . . . . . . . .

10.7 Invoking superclass constructors . . . . . . .
10.8 Using override modifiers . . . . . . . . . .
10.9 Polymorphism and dynamic binding . . . .
10.10 Declaring final members . . . . . . . . . . .
10.11 Using composition and inheritance . . . . .
10.12 Implementing above, beside, and toString
10.13 Defining a factory object . . . . . . . . . . .
10.14 Heighten and widen . . . . . . . . . . . . .
10.15 Putting it all together . . . . . . . . . . . . .
10.16 Conclusion . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

218
221

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

222
222
223
224

227
229
230
232
233
235
237
239
240
242
244
248
249

11 Scala’s Hierarchy
11.1 Scala’s class hierarchy . . . . . .
11.2 How primitives are implemented
11.3 Bottom types . . . . . . . . . . .
11.4 Conclusion . . . . . . . . . . . .

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

250
250
254
256
257

12 Traits
12.1 How traits work . . . . . . . . .
12.2 Thin versus rich interfaces . . . .
12.3 Example: Rectangular objects . .

12.4 The Ordered trait . . . . . . . .
12.5 Traits as stackable modifications .
12.6 Why not multiple inheritance? . .
12.7 To trait, or not to trait? . . . . . .
12.8 Conclusion . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

258
258
261
262
265
267

271
275
276

13 Packages and Imports
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

277


Contents
13.1
13.2
13.3
13.4
13.5
13.6
13.7

xv

Putting code in packages . . .
Concise access to related code
Imports . . . . . . . . . . . .
Implicit imports . . . . . . .
Access modifiers . . . . . . .
Package objects . . . . . . .
Conclusion . . . . . . . . . .

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

277
278
282
286
287
292
294

14 Assertions and Unit Testing

14.1 Assertions . . . . . . . . . .
14.2 Unit testing in Scala . . . . .
14.3 Informative failure reports . .
14.4 Using JUnit and TestNG . . .
14.5 Tests as specifications . . . .
14.6 Property-based testing . . . .
14.7 Organizing and running tests
14.8 Conclusion . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

295
295
297
298
300
302
305
306
308

15 Case Classes and Pattern Matching
15.1 A simple example . . . . . . .
15.2 Kinds of patterns . . . . . . . .
15.3 Pattern guards . . . . . . . . .
15.4 Pattern overlaps . . . . . . . .

15.5 Sealed classes . . . . . . . . .
15.6 The Option type . . . . . . . .
15.7 Patterns everywhere . . . . . .
15.8 A larger example . . . . . . . .
15.9 Conclusion . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

309
309
314
324

325
326
328
330
335
343

16 Working with Lists
16.1 List literals . . . . . . . . . . . . . .
16.2 The List type . . . . . . . . . . . .
16.3 Constructing lists . . . . . . . . . . .
16.4 Basic operations on lists . . . . . . .
16.5 List patterns . . . . . . . . . . . . .
16.6 First-order methods on class List . .
16.7 Higher-order methods on class List
16.8 Methods of the List object . . . . .

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

344
344
345
345
346
347
349
361
369

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents

xvi

16.9 Processing multiple lists together . . . . . . . . . . . .
16.10 Understanding Scala’s type inference algorithm . . . . .
16.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

371
372

376

17 Collections
17.1 Sequences . . . . . . . . . . . . . . . . . . . .
17.2 Sets and maps . . . . . . . . . . . . . . . . . .
17.3 Selecting mutable versus immutable collections
17.4 Initializing collections . . . . . . . . . . . . . .
17.5 Tuples . . . . . . . . . . . . . . . . . . . . . .
17.6 Conclusion . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

377
377
381
390
392
396
398

18 Stateful Objects
18.1 What makes an object stateful? . . .
18.2 Reassignable variables and properties
18.3 Case study: Discrete event simulation
18.4 A language for digital circuits . . . .
18.5 The Simulation API . . . . . . . .
18.6 Circuit Simulation . . . . . . . . . .
18.7 Conclusion . . . . . . . . . . . . . .

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

399
399
402
405
406
409
413
421

19 Type Parameterization
19.1 Functional queues . . . . . . .
19.2 Information hiding . . . . . . .
19.3 Variance annotations . . . . . .
19.4 Checking variance annotations .
19.5 Lower bounds . . . . . . . . .

19.6 Contravariance . . . . . . . . .
19.7 Object private data . . . . . . .
19.8 Upper bounds . . . . . . . . .
19.9 Conclusion . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

422
422
426
429
433
436
438
441
443
446

.
.
.
.

.

447
447
448
449
450
451

.
.
.
.
.
.
.
.
.

20 Abstract Members
20.1 A quick tour of abstract members
20.2 Type members . . . . . . . . . .
20.3 Abstract vals . . . . . . . . . . .
20.4 Abstract vars . . . . . . . . . . .
20.5 Initializing abstract vals . . . . .

.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.

.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents
20.6
20.7
20.8
20.9

20.10
20.11

Abstract types . . . . .
Path-dependent types .
Structural subtyping . .
Enumerations . . . . . .
Case study: Currencies .
Conclusion . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

459
461
464
466
468
478

21 Implicit Conversions and Parameters
21.1 Implicit conversions . . . . . . . . . .
21.2 Rules for implicits . . . . . . . . . . .
21.3 Implicit conversion to an expected type
21.4 Converting the receiver . . . . . . . .
21.5 Implicit parameters . . . . . . . . . . .
21.6 View bounds . . . . . . . . . . . . . .
21.7 When multiple conversions apply . . .
21.8 Debugging implicits . . . . . . . . . .
21.9 Conclusion . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

479
479
482
485
486
489
495
498

501
502

.
.
.
.
.

503
503
509
511
513
514

.
.
.
.
.
.
.

516
517
519
522
524
528

529
531

24 The Scala Collections API
24.1 Mutable and immutable collections . . . . . . . . . . .
24.2 Collections consistency . . . . . . . . . . . . . . . . .
24.3 Trait Traversable . . . . . . . . . . . . . . . . . . . .

532
533
535
537

22 Implementing Lists
22.1 The List class in principle
22.2 The ListBuffer class . . .
22.3 The List class in practice .
22.4 Functional on the outside .
22.5 Conclusion . . . . . . . . .

.
.
.
.
.
.

.
.
.

.
.

.
.
.
.
.
.

xvii

.
.
.
.
.

23 For Expressions Revisited
23.1 For expressions . . . . . . . . .
23.2 The n-queens problem . . . . .
23.3 Querying with for expressions
23.4 Translation of for expressions .
23.5 Going the other way . . . . . .
23.6 Generalizing for . . . . . . . .
23.7 Conclusion . . . . . . . . . . .

.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents
24.4
24.5
24.6
24.7
24.8
24.9
24.10
24.11
24.12
24.13
24.14
24.15
24.16
24.17
24.18
24.19
24.20

xviii


Trait Iterable . . . . . . . . . . . . . . . . . . . . .
The sequence traits Seq, IndexedSeq, and LinearSeq
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . .
Maps . . . . . . . . . . . . . . . . . . . . . . . . . .
Synchronized sets and maps . . . . . . . . . . . . . .
Concrete immutable collection classes . . . . . . . .
Concrete mutable collection classes . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . .
Strings . . . . . . . . . . . . . . . . . . . . . . . . .
Performance characteristics . . . . . . . . . . . . . .
Equality . . . . . . . . . . . . . . . . . . . . . . . .
Views . . . . . . . . . . . . . . . . . . . . . . . . . .
Iterators . . . . . . . . . . . . . . . . . . . . . . . .
Creating collections from scratch . . . . . . . . . . .
Conversions between Java and Scala collections . . .
Migrating from Scala 2.7 . . . . . . . . . . . . . . .
Conclusion . . . . . . . . . . . . . . . . . . . . . . .

25 The Architecture of Scala Collections
25.1 Builders . . . . . . . . . . . . .
25.2 Factoring out common operations
25.3 Integrating new collections . . .
25.4 Conclusion . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

542
546
551
557
562
564
571
578
583
584
585
587
593
601
603
605
606


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

607
608
609
614
630

26 Extractors
26.1 An example: extracting email addresses
26.2 Extractors . . . . . . . . . . . . . . .
26.3 Patterns with zero or one variables . .

26.4 Variable argument extractors . . . . . .
26.5 Extractors and sequence patterns . . .
26.6 Extractors versus case classes . . . . .
26.7 Regular expressions . . . . . . . . . .
26.8 Conclusion . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

631
631
632

635
637
640
641
642
646

27 Annotations
27.1 Why have annotations? . . . . . . . . . . . . . . . . .
27.2 Syntax of annotations . . . . . . . . . . . . . . . . . .
27.3 Standard annotations . . . . . . . . . . . . . . . . . . .

647
647
648
650

.
.
.
.

.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



Contents

xix

27.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . .
28 Working with XML
28.1 Semi-structured data . . .
28.2 XML overview . . . . . .
28.3 XML literals . . . . . . .
28.4 Serialization . . . . . . .
28.5 Taking XML apart . . . .
28.6 Deserialization . . . . . .
28.7 Loading and saving . . .
28.8 Pattern matching on XML
28.9 Conclusion . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

29 Modular Programming Using Objects
29.1 The problem . . . . . . . . . . .
29.2 A recipe application . . . . . . .
29.3 Abstraction . . . . . . . . . . . .

29.4 Splitting modules into traits . . .
29.5 Runtime linking . . . . . . . . .
29.6 Tracking module instances . . . .
29.7 Conclusion . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

30 Object Equality
30.1 Equality in Scala . . . . . . . . . . . . .
30.2 Writing an equality method . . . . . . .
30.3 Defining equality for parameterized types
30.4 Recipes for equals and hashCode . . .
30.5 Conclusion . . . . . . . . . . . . . . . .
31 Combining Scala and Java
31.1 Using Scala from Java . . . . . . .
31.2 Annotations . . . . . . . . . . . .
31.3 Existential types . . . . . . . . . .
31.4 Using synchronized . . . . . . .
31.5 Compiling Scala and Java together
31.6 Conclusion . . . . . . . . . . . . .


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

654


.
.
.
.
.
.
.
.
.

655
655
656
657
659
661
662
663
665
668

.
.
.
.
.
.
.


669
670
671
674
677
680
681
683

.
.
.
.
.

684
684
685
698
703
709

.
.
.
.
.
.

710

710
713
718
722
722
723

32 Actors and Concurrency
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

724


Contents
32.1
32.2
32.3
32.4
32.5
32.6
32.7

xx

Trouble in paradise . . . . . . . . . . . . . . . . . .
Actors and message passing . . . . . . . . . . . . .
Treating native threads as actors . . . . . . . . . . .
Better performance through thread reuse . . . . . .
Good actors style . . . . . . . . . . . . . . . . . . .
A longer example: Parallel discrete event simulation

Conclusion . . . . . . . . . . . . . . . . . . . . . .

33 Combinator Parsing
33.1 Example: Arithmetic expressions . .
33.2 Running your parser . . . . . . . . .
33.3 Basic regular expression parsers . . .
33.4 Another example: JSON . . . . . . .
33.5 Parser output . . . . . . . . . . . . .
33.6 Implementing combinator parsers . .
33.7 String literals and regular expressions
33.8 Lexing and parsing . . . . . . . . . .
33.9 Error reporting . . . . . . . . . . . .
33.10 Backtracking versus LL(1) . . . . . .
33.11 Conclusion . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

34 GUI Programming
34.1 A first Swing application . . . . . . . .
34.2 Panels and layouts . . . . . . . . . . .

34.3 Handling events . . . . . . . . . . . .
34.4 Example: Celsius/Fahrenheit converter
34.5 Conclusion . . . . . . . . . . . . . . .
35 The SCells Spreadsheet
35.1 The visual framework . . . . . . . .
35.2 Disconnecting data entry and display
35.3 Formulas . . . . . . . . . . . . . . .
35.4 Parsing formulas . . . . . . . . . . .
35.5 Evaluation . . . . . . . . . . . . . .
35.6 Operation libraries . . . . . . . . . .
35.7 Change propagation . . . . . . . . .
35.8 Conclusion . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

724
725
729
730
733
740
757

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

759
760
762
763
764
766
772
781
782
782
784

786

.
.
.
.
.

788
788
791
793
796
798

.
.
.
.
.
.
.
.

800
800
803
806
808
813

816
819
823

.
.
.
.
.

.
.
.
.
.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Contents

xxi

A Scala Scripts on Unix and Windows

825


Glossary

826

Bibliography

842

About the Authors

845

Index

846

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


List of Figures
2.1
2.2

The basic form of a function definition in Scala. . . . . . . .
The syntax of a function literal in Scala. . . . . . . . . . . .

73
79

3.1

3.2
3.3

All operations are method calls in Scala. . . . . . . . . . . .
Class hierarchy for Scala sets. . . . . . . . . . . . . . . . .
Class hierarchy for Scala maps. . . . . . . . . . . . . . . . .

84
92
94

10.1
10.2
10.3
10.4

Class diagram for ArrayElement. . . . . .
Class diagram for LineElement. . . . . . .
Class hierarchy of layout elements. . . . . .
Class hierarchy with revised LineElement.

.
.
.
.

228
233
236
240


11.1 Class hierarchy of Scala. . . . . . . . . . . . . . . . . . . .

252

12.1 Inheritance hierarchy and linearization of class Cat. . . . . .

274

14.1 ScalaTest’s graphical reporter. . . . . . . . . . . . . . . . .

307

18.1 Basic gates. . . . . . . . . . . . . . . . . . . . . . . . . . .
18.2 A half-adder circuit. . . . . . . . . . . . . . . . . . . . . . .
18.3 A full-adder circuit. . . . . . . . . . . . . . . . . . . . . . .

406
408
409

19.1 Covariance and contravariance in function type parameters. .

441

22.1 Class hierarchy for Scala lists. . . . . . . . . . . . . . . . .
22.2 The structure of the Scala lists shown in Listing 22.2. . . . .

504
508


24.1 Collection hierarchy. . . . . . . . . . . . . . . . . . . . . .

536

25.1 An example Patricia trie. . . . . . . . . . . . . . . . . . . .

625

xxii

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


List of Figures

xxiii

34.1 A simple Swing application: initial (left) and resized (right).
34.2 A reactive Swing application: initial (left) after clicks (right).
34.3 A converter between degrees Celsius and Fahrenheit. . . . .


789
791
796

35.1
35.2
35.3
35.4

801
806
812
818

A simple spreadsheet table. . .
Cells displaying themselves. .
Cells displaying their formulas.
Cells that evaluate. . . . . . .

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


List of Tables
3.1

Some List methods and usages . . . . . . . . . . . . . . .

5.1
5.2
5.3
5.4
5.5

Some basic types . . . . . . . . . . . . .
Special character literal escape sequences
Operator precedence . . . . . . . . . . .
Some rich operations . . . . . . . . . .
Rich wrapper classes . . . . . . . . . . .

.

.
.
.
.

118
122
135
138
138

12.1

Linearization of types in Cat’s hierarchy . . . . . . . . . .

275

13.1

Effects of private qualifiers on LegOfJourney.distance .

290

16.1

Basic list operations . . . . . . . . . . . . . . . . . . . . .

347

17.1

17.2
17.3
17.4

Common operations for sets . . . . . . .
Common operations for maps . . . . . .
Default immutable set implementations .
Default immutable map implementations

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

384
386
388
388

24.1
24.2
24.3
24.4
24.5
24.6

24.7
24.8
24.9
24.10

Operations in trait Traversable . . . . . . .
Operations in trait Iterable . . . . . . . . .
Operations in trait Seq . . . . . . . . . . . .
Operations in trait Buffer . . . . . . . . . .
Operations in trait Set . . . . . . . . . . . .
Operations in trait mutable.Set . . . . . . .
Operations in trait Map . . . . . . . . . . . .
Operations in trait mutable.Map . . . . . . .
Operations in trait ConcurrentMap . . . . . .
Performance characteristics of sequence types

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

539
544
548
551
552
553
558
560
577
586

xxiv

.
.
.
.
.

.
.
.
.

.
.
.

.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

88


List of Tables

xxv

24.11 Performance characteristics of set and map types . . . . . .
24.12 Operations in trait Iterator . . . . . . . . . . . . . . . .
24.13 Factory methods for sequences . . . . . . . . . . . . . . .


586
595
602

33.1

770

Summary of parser combinators . . . . . . . . . . . . . . .

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×