Programming Scala, 3rd Edition

Book description

Get up to speed on Scala--the JVM, JavaScript, and natively compiled language that offers all the benefits of functional programming, a modern object model, 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. You'll learn why Scala is ideal for building today's highly scalable, data-centric applications, while maximizing developer productivity.

While Java remains popular and Kotlin has become popular, Scala hasn't been sitting still. This third edition covers the new features in Scala 3.0 with updates throughout the book. Programming Scala is ideal for beginning to advanced developers who want a complete understanding of Scala's design philosophy and features with a thoroughly practical focus.

  • Program faster with Scala's succinct and flexible syntax
  • Dive into basic and advanced functional programming techniques
  • Build killer big data and distributed apps using Scala's functional combinators and tools like Spark and Akka
  • Create concise solutions to challenging design problems with the sophisticated type system, mixin composition with traits, pattern matching, and more

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
    1. Foreword, Third Edition
    2. Foreword, First and Second Edition
  2. Preface
    1. Welcome to Programming Scala, Third Edition
      1. How to Read This Book
    2. Welcome to Programming Scala, Second Edition
    3. Welcome to Programming Scala, First Edition
    4. Conventions Used in This Book
    5. Using Code Examples
      1. Getting the Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments for the Third Edition
    9. Acknowledgments for the Second Edition
    10. Acknowledgments for the First Edition
  3. 1. Zero to Sixty: Introducing Scala
    1. Why Scala?
      1. The Appeal of Scala
      2. Why Scala 3?
      3. Migrating to Scala 3
    2. Installing the Scala Tools You Need
    3. Building the Code Examples
    4. More Tips
      1. Using sbt
      2. Running the Scala Command-Line Tools Using sbt
    5. A Taste of Scala
    6. A Sample Application
    7. Recap and What’s Next
  4. 2. Type Less, Do More
    1. New Scala 3 Syntax—Optional Braces
    2. Semicolons
    3. Variable Declarations
    4. Ranges
    5. Partial Functions
    6. Method Declarations
      1. Method Default and Named Parameters
      2. Methods with Multiple Parameter Lists
      3. Nesting Method Definitions and Recursion
    7. Inferring Type Information
    8. Repeated Parameter Lists
    9. Language Keywords
    10. Literal Values
      1. Numeric Literals
      2. Boolean Literals
      3. Character Literals
      4. String Literals
      5. Symbol Literals
      6. Function Literals
    11. Tuples
    12. Option, Some, and None: Avoiding Nulls
      1. When You Really Can’t Avoid Nulls
    13. Sealed Class Hierarchies and Enumerations
    14. Organizing Code in Files and Namespaces
    15. Importing Types and Their Members
      1. Package Imports and Package Objects
    16. Parameterized Types Versus Abstract Type Members
    17. Recap and What’s Next
  5. 3. Rounding Out the Basics
    1. Defining Operators
    2. Allowed Characters in Identifiers
    3. Methods with Empty Parameter Lists
    4. Operator Precedence Rules
    5. Enumerations and Algebraic Data Types
    6. Interpolated Strings
    7. Scala Conditional Expressions
    8. Conditional and Comparison Operators
    9. for Comprehensions
      1. for Loops
      2. Generators
      3. Guards: Filtering Values
      4. Yielding New Values
      5. Expanded Scope and Value Definitions
    10. Scala while Loops
    11. Using try, catch, and finally Clauses
    12. Call by Name, Call by Value
    13. Lazy Values
    14. Traits: Interfaces and Mixins in Scala
    15. When new Is Optional
    16. Recap and What’s Next
  6. 4. Pattern Matching
    1. Safer Pattern Matching with Matchable
    2. Values, Variables, and Types in Matches
    3. Matching on Sequences
    4. Pattern Matching on Repeated Parameters
    5. Matching on Tuples
      1. Parameter Untupling
    6. Guards in Case Clauses
    7. Matching on Case Classes and Enums
    8. Matching on Regular Expressions
    9. Matching on Interpolated Strings
    10. Sealed Hierarchies and Exhaustive Matches
    11. Chaining Match Expressions
    12. Pattern Matching Outside Match Expressions
    13. Problems in Pattern Bindings
    14. Pattern Matching as Filtering in for Comprehensions
    15. Pattern Matching and Erasure
    16. Extractors
      1. unapply Method
      2. Alternatives to Option Return Values
      3. unapplySeq Method
      4. Implementing unapplySeq
    17. Recap and What’s Next
  7. 5. Abstracting Over Context: Type Classes and Extension Methods
    1. Four Changes
    2. Extension Methods
      1. Build Your Own String Interpolator
    3. Type Classes
      1. Scala 3 Type Classes
      2. Alias Givens
      3. Scala 2 Type Classes
    4. Scala 3 Implicit Conversions
    5. Type Class Derivation
    6. Givens and Imports
    7. Givens Scoping and Pattern Matching
    8. Resolution Rules for Givens and Extension Methods
    9. The Expression Problem
    10. Recap and What’s Next
  8. 6. Abstracting Over Context: Using Clauses
    1. Using Clauses
    2. Context Bounds
    3. Other Context Parameters
    4. Context Functions
    5. Constraining Allowed Instances
      1. Implicit Evidence
    6. Working Around Type Erasure with Using Clauses
    7. Rules for Using Clauses
    8. Improving Error Messages
    9. Recap and What’s Next
  9. 7. 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
      2. Purity Inside Versus Outside
    3. Recursion
    4. Tail Calls and Tail-Call Optimization
    5. Partially Applied Functions Versus Partial Functions
    6. Currying and Uncurrying Functions
    7. Tupled and Untupled Functions
    8. Partial Functions Versus Functions Returning Options
    9. Functional Data Structures
      1. Sequences
      2. Maps
      3. Sets
    10. Traversing, Mapping, Filtering, Folding, and Reducing
      1. Traversing
      2. Mapping
      3. Flat Mapping
      4. Filtering
      5. Folding and Reducing
      6. Left Versus Right Folding
    11. Combinators: Software’s Best Component Abstractions
    12. What About Making Copies?
    13. Recap and What’s Next
  10. 8. 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 Container Types
      1. Option as a Container?
      2. Either: An Alternative to Option
      3. Try: When There Is No Do
      4. Validated from the Cats Library
    5. Recap and What’s Next
  11. 9. Object-Oriented Programming in Scala
    1. Class and Object Basics: Review
    2. Open Versus Closed Types
      1. Classes Open for Extension
      2. Overriding Methods? The Template Method Pattern
    3. Reference Versus Value Types
    4. Opaque Types and Value Classes
      1. Opaque Type Aliases
      2. Value Classes
    5. Supertypes
    6. Constructors in Scala
      1. Calling Supertype Constructors
    7. Export Clauses
    8. Good Object-Oriented Design: A Digression
    9. Fields in Types
      1. The Uniform Access Principle
      2. Unary Methods
    10. Recap and What’s Next
  12. 10. Traits
    1. Traits as Mixins
    2. Stackable Traits
    3. Union and Intersection Types
    4. Transparent Traits
    5. Using Commas Instead of with
    6. Trait Parameters
    7. Should That Type Be a Class or Trait?
    8. Recap and What’s Next
  13. 11. Variance Behavior and Equality
    1. Parameterized Types: Variance Under Inheritance
      1. Functions Under the Hood
      2. Variance of Mutable Types
      3. Improper Variance of Java Arrays
    2. Equality of Instances
      1. The equals Method
      2. The == and != Methods
      3. The eq and ne Methods
      4. Array Equality and the sameElements Method
    3. Equality and Inheritance
    4. Multiversal Equality
    5. Case Objects and hashCode
    6. Recap and What’s Next
  14. 12. Instance Initialization and Method Resolution
    1. Linearization of a Type Hierarchy
    2. Initializing Abstract Fields
    3. Overriding Concrete Fields
    4. Abstract Type Members and Concrete Type Aliases
    5. Recap and What’s Next
  15. 13. The Scala Type Hierarchy
    1. Much Ado About Nothing (and Null)
    2. The scala Package
    3. Products, Case Classes, Tuples, and Functions
      1. Tuples and the Tuple Trait
    4. The Predef Object
      1. Implicit Conversions
      2. Type Definitions
      3. Condition Checking Methods
      4. Input and Output Methods
      5. Miscellaneous Methods
    5. Recap and What’s Next
  16. 14. The Scala Collections Library
    1. Different Groups of Collections
    2. Abstractions with Multiple Implementations
      1. The scala.collection.immutable Package
      2. The scala.collection.mutable Package
      3. The scala.collection Package
      4. The scala.collection.concurrent Package
      5. The scala.collection.convert Package
      6. The scala.collection.generic Package
    3. Construction of Instances
    4. The Iterable Abstraction
    5. Polymorphic Methods
    6. Equality for Collections
    7. Nonstrict Collections: Views
    8. Recap and What’s Next
  17. 15. Visibility Rules
    1. Public Visibility: The Default
    2. Visibility Keywords
    3. Protected Visibility
    4. Private Visibility
    5. Scoped Private and Protected Visibility
    6. Recap and What’s Next
  18. 16. Scala’s Type System, Part I
    1. Parameterized Types
    2. Abstract Type Members and Concrete Type Aliases
      1. Comparing Abstract Type Members Versus Parameterized Types
    3. Type Bounds
      1. Upper Type Bounds
      2. Lower Type Bounds
    4. Context Bounds
    5. View Bounds
    6. Intersection and Union Types
      1. Intersection Types
      2. Union Types
    7. Phantom Types
    8. Structural Types
    9. Refined Types
    10. Existential Types (Obsolete)
    11. Recap and What’s Next
  19. 17. Scala’s Type System, Part II
    1. Match Types
    2. Dependently Typed Methods
    3. Dependent Method and Dependent Function Types
    4. Dependent Typing
    5. Path-Dependent Types
      1. Using this
      2. Using super
      3. Stable Paths
    6. Self-Type Declarations
    7. Type Projections
    8. More on Singleton Types
    9. Self-Recursive Types: F-Bounded Polymorphism
    10. Higher-Kinded Types
    11. Type Lambdas
    12. Polymorphic Functions
    13. Type Wildcard Versus Placeholder
    14. Recap and What’s Next
  20. 18. Advanced Functional Programming
    1. Algebraic Data Types
      1. Sum Types Versus Product Types
      2. Properties of Algebraic Data Types
      3. Final Thoughts on Algebraic Data Types
    2. Category Theory
      1. What Is a Category?
      2. Functor
      3. The Monad Endofunctor
      4. The Semigroup and Monoid Categories
    3. Recap and What’s Next
  21. 19. Tools for Concurrency
    1. The scala.sys.process Package
    2. Futures
    3. Robust, Scalable Concurrency with Actors
      1. Akka: Actors for Scala
      2. Actors: Final Thoughts
    4. Stream Processing
    5. Recap and What’s Next
  22. 20. Dynamic Invocation in Scala
    1. Structural Types Revisited
    2. A Motivating Example: ActiveRecord in Ruby on Rails
    3. Dynamic Invocation with the Dynamic Trait
    4. DSL Considerations
    5. Recap and What’s Next
  23. 21. Domain-Specific Languages in Scala
    1. Internal DSLs
    2. External DSLs with Parser Combinators
      1. About Parser Combinators
      2. A Payroll External DSL
    3. Internal Versus External DSLs: Final Thoughts
    4. Recap and What’s Next
  24. 22. Scala Tools and Libraries
    1. Scala 3 Versions
    2. Command-Line Interface Tools
      1. Coursier
      2. Managing Java JDKs with Coursier
      3. The scalac Command-Line Tool
      4. The scala Command-Line Tool
      5. The scaladoc Command-Line Tool
      6. Other Scala Command-Line Tools
    3. Build Tools
    4. Integration with IDEs and Text Editors
    5. Using Notebook Environments with Scala
    6. Testing Tools
    7. Scala for Big Data: Apache Spark
    8. Typelevel Libraries
    9. Li Haoyi Libraries
    10. Java and Scala Interoperability
      1. Using Java Identifiers in Scala Code
      2. Scala Identifiers in Java Code
      3. Java Generics and Scala Parameterized Types
      4. Conversions Between Scala and Java Collections
      5. Java Lambdas Versus Scala Functions
      6. Annotations for JavaBean Properties and Other Purposes
    11. Recap and What’s Next
  25. 23. Application Design
    1. Recap of What We Already Know
    2. Annotations
    3. Using @main Entry Points
    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
  26. 24. Metaprogramming: Macros and Reflection
    1. Scala Compile Time Reflection
    2. Java Runtime Reflection
    3. Scala Reflect API
    4. Type Class Derivation: Implementation Details
    5. Scala 3 Metaprogramming
      1. Inline
      2. Macros
      3. Staging
    6. Wrapping Up and Looking Ahead
  27. A. Significant Indentation Versus Braces Syntax
  28. Bibliography
  29. Index

Product information

  • Title: Programming Scala, 3rd Edition
  • Author(s): Dean Wampler
  • Release date: May 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492077893