- •Contents
- •List of Figures
- •List of Tables
- •List of Listings
- •Foreword
- •Foreword to the First Edition
- •Acknowledgments
- •Introduction
- •A Scalable Language
- •A language that grows on you
- •What makes Scala scalable?
- •Why Scala?
- •Conclusion
- •First Steps in Scala
- •Conclusion
- •Next Steps in Scala
- •Conclusion
- •Classes and Objects
- •Semicolon inference
- •Singleton objects
- •A Scala application
- •Conclusion
- •Basic Types and Operations
- •Some basic types
- •Literals
- •Operators are methods
- •Arithmetic operations
- •Relational and logical operations
- •Bitwise operations
- •Object equality
- •Operator precedence and associativity
- •Rich wrappers
- •Conclusion
- •Functional Objects
- •Checking preconditions
- •Self references
- •Auxiliary constructors
- •Method overloading
- •Implicit conversions
- •A word of caution
- •Conclusion
- •Built-in Control Structures
- •If expressions
- •While loops
- •For expressions
- •Match expressions
- •Variable scope
- •Conclusion
- •Functions and Closures
- •Methods
- •Local functions
- •Short forms of function literals
- •Placeholder syntax
- •Partially applied functions
- •Closures
- •Special function call forms
- •Tail recursion
- •Conclusion
- •Control Abstraction
- •Reducing code duplication
- •Simplifying client code
- •Currying
- •Writing new control structures
- •Conclusion
- •Composition and Inheritance
- •A two-dimensional layout library
- •Abstract classes
- •Extending classes
- •Invoking superclass constructors
- •Polymorphism and dynamic binding
- •Using composition and inheritance
- •Heighten and widen
- •Putting it all together
- •Conclusion
- •How primitives are implemented
- •Bottom types
- •Conclusion
- •Traits
- •How traits work
- •Thin versus rich interfaces
- •Example: Rectangular objects
- •The Ordered trait
- •Why not multiple inheritance?
- •To trait, or not to trait?
- •Conclusion
- •Packages and Imports
- •Putting code in packages
- •Concise access to related code
- •Imports
- •Implicit imports
- •Package objects
- •Conclusion
- •Assertions and Unit Testing
- •Assertions
- •Unit testing in Scala
- •Informative failure reports
- •Using JUnit and TestNG
- •Property-based testing
- •Organizing and running tests
- •Conclusion
- •Case Classes and Pattern Matching
- •A simple example
- •Kinds of patterns
- •Pattern guards
- •Pattern overlaps
- •Sealed classes
- •The Option type
- •Patterns everywhere
- •A larger example
- •Conclusion
- •Working with Lists
- •List literals
- •The List type
- •Constructing lists
- •Basic operations on lists
- •List patterns
- •First-order methods on class List
- •Methods of the List object
- •Processing multiple lists together
- •Conclusion
- •Collections
- •Sequences
- •Sets and maps
- •Selecting mutable versus immutable collections
- •Initializing collections
- •Tuples
- •Conclusion
- •Stateful Objects
- •What makes an object stateful?
- •Reassignable variables and properties
- •Case study: Discrete event simulation
- •A language for digital circuits
- •The Simulation API
- •Circuit Simulation
- •Conclusion
- •Type Parameterization
- •Functional queues
- •Information hiding
- •Variance annotations
- •Checking variance annotations
- •Lower bounds
- •Contravariance
- •Object private data
- •Upper bounds
- •Conclusion
- •Abstract Members
- •A quick tour of abstract members
- •Type members
- •Abstract vals
- •Abstract vars
- •Initializing abstract vals
- •Abstract types
- •Path-dependent types
- •Structural subtyping
- •Enumerations
- •Case study: Currencies
- •Conclusion
- •Implicit Conversions and Parameters
- •Implicit conversions
- •Rules for implicits
- •Implicit conversion to an expected type
- •Converting the receiver
- •Implicit parameters
- •View bounds
- •When multiple conversions apply
- •Debugging implicits
- •Conclusion
- •Implementing Lists
- •The List class in principle
- •The ListBuffer class
- •The List class in practice
- •Functional on the outside
- •Conclusion
- •For Expressions Revisited
- •For expressions
- •The n-queens problem
- •Querying with for expressions
- •Translation of for expressions
- •Going the other way
- •Conclusion
- •The Scala Collections API
- •Mutable and immutable collections
- •Collections consistency
- •Trait Traversable
- •Trait Iterable
- •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
- •The Architecture of Scala Collections
- •Builders
- •Factoring out common operations
- •Integrating new collections
- •Conclusion
- •Extractors
- •An example: extracting email addresses
- •Extractors
- •Patterns with zero or one variables
- •Variable argument extractors
- •Extractors and sequence patterns
- •Extractors versus case classes
- •Regular expressions
- •Conclusion
- •Annotations
- •Why have annotations?
- •Syntax of annotations
- •Standard annotations
- •Conclusion
- •Working with XML
- •Semi-structured data
- •XML overview
- •XML literals
- •Serialization
- •Taking XML apart
- •Deserialization
- •Loading and saving
- •Pattern matching on XML
- •Conclusion
- •Modular Programming Using Objects
- •The problem
- •A recipe application
- •Abstraction
- •Splitting modules into traits
- •Runtime linking
- •Tracking module instances
- •Conclusion
- •Object Equality
- •Equality in Scala
- •Writing an equality method
- •Recipes for equals and hashCode
- •Conclusion
- •Combining Scala and Java
- •Using Scala from Java
- •Annotations
- •Existential types
- •Using synchronized
- •Compiling Scala and Java together
- •Conclusion
- •Actors and Concurrency
- •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
- •Combinator Parsing
- •Example: Arithmetic expressions
- •Running your parser
- •Basic regular expression parsers
- •Another example: JSON
- •Parser output
- •Implementing combinator parsers
- •String literals and regular expressions
- •Lexing and parsing
- •Error reporting
- •Backtracking versus LL(1)
- •Conclusion
- •GUI Programming
- •Panels and layouts
- •Handling events
- •Example: Celsius/Fahrenheit converter
- •Conclusion
- •The SCells Spreadsheet
- •The visual framework
- •Disconnecting data entry and display
- •Formulas
- •Parsing formulas
- •Evaluation
- •Operation libraries
- •Change propagation
- •Conclusion
- •Scala Scripts on Unix and Windows
- •Glossary
- •Bibliography
- •About the Authors
- •Index
List of Figures
2.1 |
The basic form of a function definition in Scala. . . . . . . . |
73 |
2.2 |
The syntax of a function literal in Scala. . . . . . . . . . . . |
79 |
3.1 |
All operations are method calls in Scala. . . . . . . . . . . . |
84 |
3.2 |
Class hierarchy for Scala sets. . . . . . . . . . . . . . . . . |
92 |
3.3 |
Class hierarchy for Scala maps. . . . . . . . . . . . . . . . . |
94 |
10.1 |
Class diagram for ArrayElement. . . . . . . . . . . . . . . |
228 |
10.2 |
Class diagram for LineElement. . . . . . . . . . . . . . . . |
233 |
10.3 |
Class hierarchy of layout elements. . . . . . . . . . . . . . . |
236 |
10.4 |
Class hierarchy with revised LineElement. . . . . . . . . . |
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. . . . . . . . . . . . . . . . . . . . . . . . . . . |
406 |
18.2 |
A half-adder circuit. . . . . . . . . . . . . . . . . . . . . . . |
408 |
18.3 |
A full-adder circuit. . . . . . . . . . . . . . . . . . . . . . . |
409 |
19.1 |
Covariance and contravariance in function type parameters. . |
441 |
22.1 |
Class hierarchy for Scala lists. . . . . . . . . . . . . . . . . |
504 |
22.2 |
The structure of the Scala lists shown in Listing 22.2. . . . . |
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). |
789 |
34.2 |
A reactive Swing application: initial (left) after clicks (right). |
791 |
34.3 |
A converter between degrees Celsius and Fahrenheit. . . . . |
796 |
35.1 |
A simple spreadsheet table. . . . . . . . . . . . . . . . . . . |
801 |
35.2 |
Cells displaying themselves. . . . . . . . . . . . . . . . . . |
806 |
35.3 |
Cells displaying their formulas. . . . . . . . . . . . . . . . . |
812 |
35.4 |
Cells that evaluate. . . . . . . . . . . . . . . . . . . . . . . |
818 |
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index
List of Tables
3.1 |
Some List methods and usages . . . . . . . . . . . . . . . |
88 |
5.1 |
Some basic types . . . . . . . . . . . . . . . . . . . . . . . |
118 |
5.2 |
Special character literal escape sequences . . . . . . . . . . |
122 |
5.3 |
Operator precedence . . . . . . . . . . . . . . . . . . . . . |
135 |
5.4 |
Some rich operations . . . . . . . . . . . . . . . . . . . . |
138 |
5.5 |
Rich wrapper classes . . . . . . . . . . . . . . . . . . . . . |
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 |
Common operations for sets . . . . . . . . . . . . . . . . . |
384 |
17.2 |
Common operations for maps . . . . . . . . . . . . . . . . |
386 |
17.3 |
Default immutable set implementations . . . . . . . . . . . |
388 |
17.4 |
Default immutable map implementations . . . . . . . . . . |
388 |
24.1 |
Operations in trait Traversable . . . . . . . . . . . . . . |
539 |
24.2 |
Operations in trait Iterable . . . . . . . . . . . . . . . . |
544 |
24.3 |
Operations in trait Seq . . . . . . . . . . . . . . . . . . . |
548 |
24.4 |
Operations in trait Buffer . . . . . . . . . . . . . . . . . |
551 |
24.5 |
Operations in trait Set . . . . . . . . . . . . . . . . . . . |
552 |
24.6 |
Operations in trait mutable.Set . . . . . . . . . . . . . . |
553 |
24.7 |
Operations in trait Map . . . . . . . . . . . . . . . . . . . |
558 |
24.8 |
Operations in trait mutable.Map . . . . . . . . . . . . . . |
560 |
24.9 |
Operations in trait ConcurrentMap . . . . . . . . . . . . . |
577 |
24.10 |
Performance characteristics of sequence types . . . . . . . |
586 |
xxiv
|
List of Tables |
xxv |
24.11 |
Performance characteristics of set and map types . . . . . . |
586 |
24.12 |
Operations in trait Iterator . . . . . . . . . . . . . . . . |
595 |
24.13 |
Factory methods for sequences . . . . . . . . . . . . . . . |
602 |
33.1 |
Summary of parser combinators . . . . . . . . . . . . . . . |
770 |
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index