Scala is an object-oriented programming language for the Java Virtual Machine. In addition to being object-oriented, Scala is also a functional language, and combines the best approaches to OO and functional programming. In Italian, Scala means a stairway, or steps indeed, Scala lets you step up to a programming environment that incorporates some of the best recent thinking in programming language design while also letting you use all your existing Java code. Artima is very pleased to publish a new edition of the best-selling book on Scala, written by the designer of the language, Martin Odersky. Co-authored by Lex Spoon and Bill Venners, this book takes a step-by-step tutorial approach to teaching you Scala. Starting with the fundamental elements of the language, Programming in Scala introduces functional programming from the practitioner's perspective, and describes advanced language features that can make you a better, more productive developer.
Author(s): Martin Odersky, Lex Spoon, Bill Venners
Edition: 2
Publisher: Artima Inc
Year: 2010
Language: English
Pages: 883
Tags: Библиотека;Компьютерная литература;Scala;
Contents......Page 11
List of Figures......Page 22
List of Tables......Page 24
List of Listings......Page 26
Foreword......Page 34
Foreword to the First Edition......Page 36
Acknowledgments......Page 38
Introduction......Page 41
A Scalable Language......Page 49
A language that grows on you......Page 50
What makes Scala scalable?......Page 55
Why Scala?......Page 58
Scala's roots......Page 65
Conclusion......Page 67
Learn to use the Scala interpreter......Page 68
Define some variables......Page 70
Define some functions......Page 72
Write some Scala scripts......Page 74
Loop with while; decide with if......Page 75
Iterate with foreach and for......Page 77
Conclusion......Page 80
Parameterize arrays with types......Page 81
Use lists......Page 85
Use tuples......Page 90
Use sets and maps......Page 91
Learn to recognize the functional style......Page 96
Read lines from a file......Page 99
Conclusion......Page 102
Classes, fields, and methods......Page 103
Semicolon inference......Page 108
Singleton objects......Page 109
A Scala application......Page 112
The Application trait......Page 115
Conclusion......Page 116
Some basic types......Page 117
Literals......Page 118
Operators are methods......Page 125
Arithmetic operations......Page 128
Relational and logical operations......Page 129
Bitwise operations......Page 131
Object equality......Page 132
Operator precedence and associativity......Page 134
Conclusion......Page 137
A specification for class Rational......Page 139
Constructing a Rational......Page 140
Reimplementing the toString method......Page 142
Adding fields......Page 143
Self references......Page 145
Auxiliary constructors......Page 146
Private fields and methods......Page 148
Defining operators......Page 149
Identifiers in Scala......Page 151
Method overloading......Page 154
Implicit conversions......Page 156
Conclusion......Page 157
Built-in Control Structures......Page 159
If expressions......Page 160
While loops......Page 161
For expressions......Page 164
Exception handling with try expressions......Page 169
Match expressions......Page 173
Living without break and continue......Page 175
Variable scope......Page 177
Refactoring imperative-style code......Page 181
Conclusion......Page 183
Methods......Page 184
Local functions......Page 186
First-class functions......Page 188
Short forms of function literals......Page 190
Placeholder syntax......Page 191
Partially applied functions......Page 192
Closures......Page 195
Special function call forms......Page 199
Tail recursion......Page 202
Conclusion......Page 206
Reducing code duplication......Page 207
Simplifying client code......Page 211
Currying......Page 213
Writing new control structures......Page 215
By-name parameters......Page 218
Conclusion......Page 221
A two-dimensional layout library......Page 222
Abstract classes......Page 223
Defining parameterless methods......Page 224
Extending classes......Page 227
Overriding methods and fields......Page 229
Defining parametric fields......Page 230
Invoking superclass constructors......Page 232
Using override modifiers......Page 233
Polymorphism and dynamic binding......Page 235
Declaring final members......Page 237
Using composition and inheritance......Page 239
Implementing above, beside, and toString......Page 240
Defining a factory object......Page 242
Heighten and widen......Page 244
Putting it all together......Page 248
Conclusion......Page 249
Scala's class hierarchy......Page 250
How primitives are implemented......Page 254
Bottom types......Page 256
Conclusion......Page 257
How traits work......Page 258
Thin versus rich interfaces......Page 261
Example: Rectangular objects......Page 262
The Ordered trait......Page 265
Traits as stackable modifications......Page 267
Why not multiple inheritance?......Page 271
To trait, or not to trait?......Page 275
Conclusion......Page 276
Putting code in packages......Page 277
Concise access to related code......Page 278
Imports......Page 282
Implicit imports......Page 286
Access modifiers......Page 287
Package objects......Page 292
Conclusion......Page 294
Assertions......Page 295
Unit testing in Scala......Page 297
Informative failure reports......Page 298
Using JUnit and TestNG......Page 300
Tests as specifications......Page 302
Property-based testing......Page 305
Organizing and running tests......Page 306
Conclusion......Page 308
A simple example......Page 309
Kinds of patterns......Page 314
Pattern guards......Page 324
Pattern overlaps......Page 325
Sealed classes......Page 326
The Option type......Page 328
Patterns everywhere......Page 330
A larger example......Page 335
Conclusion......Page 343
List literals......Page 344
Constructing lists......Page 345
Basic operations on lists......Page 346
List patterns......Page 347
First-order methods on class List......Page 349
Higher-order methods on class List......Page 361
Methods of the List object......Page 369
Processing multiple lists together......Page 371
Understanding Scala's type inference algorithm......Page 372
Conclusion......Page 376
Sequences......Page 377
Sets and maps......Page 381
Selecting mutable versus immutable collections......Page 390
Initializing collections......Page 392
Tuples......Page 396
Conclusion......Page 398
What makes an object stateful?......Page 399
Reassignable variables and properties......Page 402
Case study: Discrete event simulation......Page 405
A language for digital circuits......Page 406
The Simulation API......Page 409
Circuit Simulation......Page 413
Conclusion......Page 421
Functional queues......Page 422
Information hiding......Page 426
Variance annotations......Page 429
Checking variance annotations......Page 433
Lower bounds......Page 436
Contravariance......Page 438
Object private data......Page 441
Upper bounds......Page 443
Conclusion......Page 446
A quick tour of abstract members......Page 447
Type members......Page 448
Abstract vals......Page 449
Abstract vars......Page 450
Initializing abstract vals......Page 451
Abstract types......Page 459
Path-dependent types......Page 461
Structural subtyping......Page 464
Enumerations......Page 466
Case study: Currencies......Page 468
Conclusion......Page 478
Implicit conversions......Page 479
Rules for implicits......Page 482
Implicit conversion to an expected type......Page 485
Converting the receiver......Page 486
Implicit parameters......Page 489
View bounds......Page 495
When multiple conversions apply......Page 498
Debugging implicits......Page 501
Conclusion......Page 502
The List class in principle......Page 503
The ListBuffer class......Page 509
The List class in practice......Page 511
Functional on the outside......Page 513
Conclusion......Page 514
For Expressions Revisited......Page 516
For expressions......Page 517
The n-queens problem......Page 519
Querying with for expressions......Page 522
Translation of for expressions......Page 524
Going the other way......Page 528
Generalizing for......Page 529
Conclusion......Page 531
The Scala Collections API......Page 532
Mutable and immutable collections......Page 533
Collections consistency......Page 535
Trait Traversable......Page 537
Trait Iterable......Page 542
The sequence traits Seq, IndexedSeq, and LinearSeq......Page 546
Sets......Page 551
Maps......Page 557
Synchronized sets and maps......Page 562
Concrete immutable collection classes......Page 564
Concrete mutable collection classes......Page 571
Arrays......Page 578
Strings......Page 583
Performance characteristics......Page 584
Equality......Page 585
Views......Page 587
Iterators......Page 593
Creating collections from scratch......Page 601
Conversions between Java and Scala collections......Page 603
Migrating from Scala 2.7......Page 605
Conclusion......Page 606
The Architecture of Scala Collections......Page 607
Builders......Page 608
Factoring out common operations......Page 609
Integrating new collections......Page 614
Conclusion......Page 630
An example: extracting email addresses......Page 631
Extractors......Page 632
Patterns with zero or one variables......Page 635
Variable argument extractors......Page 637
Extractors and sequence patterns......Page 640
Extractors versus case classes......Page 641
Regular expressions......Page 642
Conclusion......Page 646
Why have annotations?......Page 647
Syntax of annotations......Page 648
Standard annotations......Page 650
Conclusion......Page 654
Semi-structured data......Page 655
XML overview......Page 656
XML literals......Page 657
Serialization......Page 659
Taking XML apart......Page 661
Deserialization......Page 662
Loading and saving......Page 663
Pattern matching on XML......Page 665
Conclusion......Page 668
Modular Programming Using Objects......Page 669
The problem......Page 670
A recipe application......Page 671
Abstraction......Page 674
Splitting modules into traits......Page 677
Runtime linking......Page 680
Tracking module instances......Page 681
Conclusion......Page 683
Equality in Scala......Page 684
Writing an equality method......Page 685
Defining equality for parameterized types......Page 698
Recipes for equals and hashCode......Page 703
Conclusion......Page 709
Using Scala from Java......Page 710
Annotations......Page 713
Existential types......Page 718
Compiling Scala and Java together......Page 722
Conclusion......Page 723
Trouble in paradise......Page 724
Actors and message passing......Page 725
Treating native threads as actors......Page 729
Better performance through thread reuse......Page 730
Good actors style......Page 733
A longer example: Parallel discrete event simulation......Page 740
Conclusion......Page 757
Combinator Parsing......Page 759
Example: Arithmetic expressions......Page 760
Running your parser......Page 762
Basic regular expression parsers......Page 763
Another example: JSON......Page 764
Parser output......Page 766
Implementing combinator parsers......Page 772
String literals and regular expressions......Page 781
Error reporting......Page 782
Backtracking versus LL(1)......Page 784
Conclusion......Page 786
A first Swing application......Page 788
Panels and layouts......Page 791
Handling events......Page 793
Example: Celsius/Fahrenheit converter......Page 796
Conclusion......Page 798
The visual framework......Page 800
Disconnecting data entry and display......Page 803
Formulas......Page 806
Parsing formulas......Page 808
Evaluation......Page 813
Operation libraries......Page 816
Change propagation......Page 819
Conclusion......Page 823
Scala Scripts on Unix and Windows......Page 825
Glossary......Page 826
Bibliography......Page 842
About the Authors......Page 845
Index......Page 846