You are previewing Programming Scala, 2nd Edition.
O'Reilly logo
Programming Scala, 2nd Edition

Book Description

Get up to speed on Scala, the JVM language that offers all the benefits of a modern object model, functional programming, and an advanced type system. Packed with code examples, this comprehensive book shows you how to be productive with the language and ecosystem right away, and explains why Scala is ideal for today's highly scalable, data-centric applications that support concurrency and distribution. This second edition covers recent language features, with new chapters on pattern matching, comprehensions, and advanced functional programming.

Table of Contents

  1. Dedication
  2. Foreword
  3. Preface
    1. Welcome to Programming Scala, Second Edition
      1. How to Read This Book
      2. What Isn’t Covered?
    2. Welcome to Programming Scala, First Edition
    3. Conventions Used in This Book
    4. Using Code Examples
      1. Getting the Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments for the Second Edition
    8. Acknowledgments for the First Edition
  4. 1. Zero to Sixty: Introducing Scala
    1. Why Scala?
      1. The Seductions of Scala
      2. What About Java 8?
    2. Installing Scala
      1. Using SBT
      2. Running the Scala Command-Line Tools
      3. Running the Scala REPL in IDEs
    3. A Taste of Scala
    4. A Taste of Concurrency
    5. Recap and What’s Next
  5. 2. Type Less, Do More
    1. Semicolons
    2. Variable Declarations
    3. Ranges
    4. Partial Functions
    5. Method Declarations
      1. Method Default and Named Arguments
      2. Methods with Multiple Argument Lists
      3. A Taste of Futures
      4. Nesting Method Definitions and Recursion
    6. Inferring Type Information
    7. Reserved Words
    8. Literal Values
      1. Integer Literals
      2. Floating-Point Literals
      3. Boolean Literals
      4. Character Literals
      5. String Literals
      6. Symbol Literals
      7. Function Literals
      8. Tuple Literals
    9. Option, Some, and None: Avoiding nulls
    10. Sealed Class Hierarchies
    11. Organizing Code in Files and Namespaces
    12. Importing Types and Their Members
      1. Imports Are Relative
      2. Package Objects
    13. Abstract Types Versus Parameterized Types
    14. Recap and What’s Next
  6. 3. Rounding Out the Basics
    1. Operator Overloading?
      1. Syntactic Sugar
    2. Methods with Empty Argument Lists
    3. Precedence Rules
    4. Domain-Specific Languages
    5. Scala if Statements
    6. Scala for Comprehensions
      1. for Loops
      2. Generator Expressions
      3. Guards: Filtering Values
      4. Yielding
      5. Expanded Scope and Value Definitions
    7. Other Looping Constructs
      1. Scala while Loops
      2. Scala do-while Loops
    8. Conditional Operators
    9. Using try, catch, and finally Clauses
    10. Call by Name, Call by Value
    11. lazy val
    12. Enumerations
    13. Interpolated Strings
    14. Traits: Interfaces and “Mixins” in Scala
    15. Recap and What’s Next
  7. 4. Pattern Matching
    1. A Simple Match
    2. Values, Variables, and Types in Matches
    3. Matching on Sequences
    4. Matching on Tuples
    5. Guards in case Clauses
    6. Matching on case Classes
      1. unapply Method
      2. unapplySeq Method
    7. Matching on Variable Argument Lists
    8. Matching on Regular Expressions
    9. More on Binding Variables in case Clauses
    10. More on Type Matching
    11. Sealed Hierarchies and Exhaustive Matches
    12. Other Uses of Pattern Matching
    13. Concluding Remarks on Pattern Matching
    14. Recap and What’s Next
  8. 5. Implicits
    1. Implicit Arguments
      1. Using implicitly
    2. Scenarios for Implicit Arguments
      1. Execution Contexts
      2. Capabilities
      3. Constraining Allowed Instances
      4. Implicit Evidence
      5. Working Around Erasure
      6. Improving Error Messages
      7. Phantom Types
      8. Rules for Implicit Arguments
    3. Implicit Conversions
      1. Build Your Own String Interpolator
      2. The Expression Problem
    4. Type Class Pattern
    5. Technical Issues with Implicits
    6. Implicit Resolution Rules
    7. Scala’s Built-in Implicits
    8. Wise Use of Implicits
    9. Recap and What’s Next
  9. 6. Functional Programming in Scala
    1. What Is Functional Programming?
      1. Functions in Mathematics
      2. Variables That Aren’t
    2. Functional Programming in Scala
      1. Anonymous Functions, Lambdas, and Closures
        1. Methods as Functions
      2. Purity Inside Versus Outside
    3. Recursion
    4. Tail Calls and Tail-Call Optimization
      1. Trampoline for Tail Calls
    5. Partially Applied Functions Versus Partial Functions
    6. Currying and Other Transformations on Functions
    7. Functional Data Structures
      1. Sequences
      2. Maps
      3. Sets
    8. Traversing, Mapping, Filtering, Folding, and Reducing
      1. Traversal
      2. Mapping
      3. Flat Mapping
      4. Filtering
      5. Folding and Reducing
    9. Left Versus Right Traversals
      1. Tail Recursion Versus Traversals of Infinite Collections
    10. Combinators: Software’s Best Component Abstractions
    11. What About Making Copies?
    12. Recap and What’s Next
  10. 7. for Comprehensions in Depth
    1. Recap: The Elements of for Comprehensions
    2. for Comprehensions: Under the Hood
    3. Translation Rules of for Comprehensions
    4. Options and Other Container Types
      1. Option as a Container
      2. Either: A Logical Extension to Option
        1. Throwing exceptions versus returning Either values
      3. Try: When There Is No Do
      4. Scalaz Validation
    5. Recap and What’s Next
  11. 8. Object-Oriented Programming in Scala
    1. Class and Object Basics
    2. Reference Versus Value Types
    3. Value Classes
    4. Parent Types
    5. Constructors in Scala
    6. Fields in Classes
      1. The Uniform Access Principle
      2. Unary Methods
    7. Validating Input
    8. Calling Parent Class Constructors (and Good Object-Oriented Design)
      1. Good Object-Oriented Design: A Digression
    9. Nested Types
    10. Recap and What’s Next
  12. 9. Traits
    1. Interfaces in Java 8
    2. Traits as Mixins
    3. Stackable Traits
    4. Constructing Traits
    5. Class or Trait?
    6. Recap and What’s Next
  13. 10. The Scala Object System, Part I
    1. Parameterized Types: Variance Under Inheritance
      1. Functions Under the Hood
      2. Variance of Mutable Types
      3. Variance in Scala Versus Java
    2. The Scala Type Hierarchy
    3. Much Ado About Nothing (and Null)
    4. Products, Case Classes, and Tuples
    5. The Predef Object
      1. Implicit Conversions
      2. Type Definitions
      3. Condition Checking Methods
      4. Input and Output Methods
      5. Miscellaneous Methods
    6. Equality of Objects
      1. The equals Method
      2. The == and != Methods
      3. The eq and ne Methods
      4. Array Equality and the sameElements Method
    7. Recap and What’s Next
  14. 11. The Scala Object System, Part II
    1. Overriding Members of Classes and Traits
      1. Avoid Overriding Concrete Members
      2. Attempting to Override final Declarations
      3. Overriding Abstract and Concrete Methods
      4. Overriding Abstract and Concrete Fields
        1. Overriding fields in traits
        2. Overriding fields in classes
      5. Overriding Abstract Types
      6. When Accessor Methods and Fields Are Indistinguishable: The Uniform Access Principle
    2. Linearization of an Object’s Hierarchy
    3. Recap and What’s Next
  15. 12. The Scala Collections Library
    1. Generic, Mutable, Immutable, Concurrent, and Parallel Collections, Oh My!
      1. The scala.collection Package
      2. The collection.concurrent Package
      3. The collection.convert Package
      4. The collection.generic Package
      5. The collection.immutable Package
      6. The scala.collection.mutable Package
      7. The scala.collection.parallel Package
    2. Choosing a Collection
    3. Design Idioms in the Collections Library
      1. Builder
      2. CanBuildFrom
      3. Like Traits
    4. Specialization for Value Types
      1. Miniboxing
    5. Recap and What’s Next
  16. 13. Visibility Rules
    1. Public Visibility: The Default
    2. Visibility Keywords
    3. Public Visibility
    4. Protected Visibility
    5. Private Visibility
    6. Scoped Private and Protected Visibility
    7. Final Thoughts on Visibility
    8. Recap and What’s Next
  17. 14. Scala’s Type System, Part I
    1. Parameterized Types
      1. Variance Annotations
      2. Type Constructors
      3. Type Parameter Names
    2. Type Bounds
      1. Upper Type Bounds
      2. Lower Type Bounds
    3. Context Bounds
    4. View Bounds
    5. Understanding Abstract Types
      1. Comparing Abstract Types and Parameterized Types
    6. Self-Type Annotations
    7. Structural Types
    8. Compound Types
      1. Type Refinements
    9. Existential Types
    10. Recap and What’s Next
  18. 15. Scala’s Type System, Part II
    1. Path-Dependent Types
      1. C.this
      2. C.super
      3. path.x
    2. Dependent Method Types
    3. Type Projections
      1. Singleton Types
    4. Types for Values
      1. Tuple Types
      2. Function Types
      3. Infix Types
    5. Higher-Kinded Types
    6. Type Lambdas
    7. Self-Recursive Types: F-Bounded Polymorphism
    8. Recap and What’s Next
  19. 16. Advanced Functional Programming
    1. Algebraic Data Types
      1. Sum Types Versus Product Types
      2. Properties of Algebraic Data Types
      3. Final Thought on Algebraic Data Types
    2. Category Theory
      1. About Categories
      2. The Functor Category
      3. The Monad Category
      4. The Importance of Monad
    3. Recap and What’s Next
  20. 17. Tools for Concurrency
    1. The scala.sys.process Package
    2. Futures
      1. Async
    3. Robust, Scalable Concurrency with Actors
    4. Akka: Actors for Scala
      1. Actors: Final Thoughts
    5. Pickling and Spores
    6. Reactive Programming
    7. Recap and What’s Next
  21. 18. Scala for Big Data
    1. Big Data: A Brief History
    2. Improving MapReduce with Scala
    3. Moving Beyond MapReduce
    4. Categories for Mathematics
    5. A List of Scala-Based Data Tools
    6. Recap and What’s Next
  22. 19. Dynamic Invocation in Scala
    1. A Motivating Example: ActiveRecord in Ruby on Rails
    2. Dynamic Invocation in Scala with the Dynamic Trait
    3. DSL Considerations
    4. Recap and What’s Next
  23. 20. Domain-Specific Languages in Scala
    1. Examples: XML and JSON DSLs for Scala
    2. Internal DSLs
    3. External DSLs with Parser Combinators
      1. About Parser Combinators
      2. A Payroll External DSL
    4. Internal Versus External DSLs: Final Thoughts
    5. Recap and What’s Next
  24. 21. Scala Tools and Libraries
    1. Command-Line Tools
      1. scalac Command-Line Tool
      2. The scala Command-Line Tool
        1. Limitations of scala versus scalac
      3. The scalap and javap Command-Line Tools
      4. The scaladoc Command-Line Tool
      5. The fsc Command-Line Tool
    2. Build Tools
      1. SBT, the Standard Build Tool for Scala
      2. Other Build Tools
    3. Integration with IDEs and Text Editors
      1. Text Editors
    4. Test-Driven Development in Scala
    5. Third-Party Libraries
    6. Recap and What’s Next
  25. 22. Java Interoperability
    1. Using Java Names in Scala Code
    2. Java and Scala Generics
    3. JavaBean Properties
    4. AnyVal Types and Java Primitives
    5. Scala Names in Java Code
    6. Recap and What’s Next
  26. 23. Application Design
    1. Recap of What We Already Know
    2. Annotations
    3. Traits as Modules
    4. Design Patterns
      1. Creational Patterns
      2. Structural Patterns
      3. Behavioral Patterns
    5. Better Design with Design by Contract
    6. The Parthenon Architecture
    7. Recap and What’s Next
  27. 24. Metaprogramming: Macros and Reflection
    1. Tools for Understanding Types
    2. Runtime Reflection
      1. Reflecting on Types
      2. Class Tags, Type Tags, and Manifests
    3. Scala’s Advanced Runtime Reflection API
    4. Macros
      1. A Macro Example: Enforcing Invariants
      2. Final Thoughts on Macros
    5. Wrapping Up and Looking Ahead
  28. A. References
  29. Index
  30. Colophon
  31. Copyright