Scala for the Impatient, 3rd Edition

Book description

Scala 3--A Clear, Concise Guide

Scala 3 is concise, consistent, flexible, robust, and efficient, but there's a lot to learn and navigating features and improvements can be challenging. Scala for the Impatient, Third Edition, is a complete yet concise guide that reflects the major enhancements of Scala 3, from improved syntax and revamped type system to powerful contextual abstractions.

This indispensable tutorial offers a faster, easier pathway for learning today's Scala. Horstmann--author of the programming classic, Core Java--covers everything working developers need to know, focusing on hands-on solutions, not academic theory. Given the size and scope of Scala 3, there's plenty to cover but it's presented in small chunks organized for quick access and easy understanding, with plenty of practical insights and focused sample code.

  • Get started quickly with Scala 3 interpreter, syntax, tools, and current usage

  • Master core language features: functions, arrays, maps, tuples, packages, imports, exception handling, and more

  • Design and build better object-oriented code with Scala 3

  • Use Scala for real-world programming tasks: working with files, regular expressions, and processes

  • Work with higher-order functions and the powerful Scala collections library

  • Create concurrent programs with Scala futures

  • Understand the Scala type system, including revamped enums, intersection and union types, and enhanced type inference

  • Use contextual abstractions to easily extend class hierarchies, enrich existing classes, perform automatic conversions, and elegantly hide tedious details

  • Apply advanced "power tools" such as annotations and given values

  • Discover how to "program with types," analyzing and generating types at compile time

  • Get a taste of what's now possible with Scala macros

If you're a Java, Python, C++, or C# programmer who's new to Scala or functional programming--or even if you've already used earlier versions of Scala--this guide will help you write code that's more robust, more efficient, and more secure.

Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.

Table of contents

  1. Cover Page
  2. About This eBook
  3. Halftitle Page
  4. Title Page
  5. Copyright Page
  6. Pearson’s Commitment to Diversity, Equity, and Inclusion
  7. Dedication Page
  8. Contents
  9. Foreword to the First Edition
  10. Preface
  11. About the Author
  12. Chapter 1. The Basics
    1. 1.1 The Scala Interpreter
    2. 1.2 Declaring Values and Variables
    3. 1.3 Commonly Used Types
    4. 1.4 Arithmetic and Operator Overloading
    5. 1.5 More about Calling Methods
    6. 1.6 The apply Method
    7. 1.7 Scaladoc
    8. Exercises
  13. Chapter 2. Control Structures and Functions
    1. 2.1 Conditional Expressions
    2. 2.2 Statement Termination
    3. 2.3 Block Expressions and Assignments
    4. 2.4 Input and Output
    5. 2.5 Loops
    6. 2.6 More about the for Loop
    7. 2.7 Functions
    8. 2.8 Default and Named Arguments
    9. 2.9 Variable Arguments
    10. 2.10 The Main Function
    11. 2.11 Functions without Parameters
    12. 2.12 Lazy Values
    13. 2.13 Exceptions
    14. Exercises
  14. Chapter 3. Working with Arrays
    1. 3.1 Fixed-Length Arrays
    2. 3.2 Variable-Length Arrays: Array Buffers
    3. 3.3 Traversing Arrays and Array Buffers
    4. 3.4 Transforming Arrays
    5. 3.5 Common Algorithms
    6. 3.6 Deciphering Scaladoc
    7. 3.7 Multidimensional Arrays
    8. 3.8 Interoperating with Java
    9. Exercises
  15. Chapter 4. Maps, Options, and Tuples
    1. 4.1 Constructing a Map
    2. 4.2 Accessing Map Values
    3. 4.3 Updating Map Values
    4. 4.4 Iterating over Maps
    5. 4.5 Linked and Sorted Maps
    6. 4.6 Interoperating with Java
    7. 4.7 The Option Type
    8. 4.8 Tuples
    9. 4.9 Zipping
    10. Exercises
  16. Chapter 5. Classes
    1. 5.1 Simple Classes and Parameterless Methods
    2. 5.2 Properties with Getters and Setters
    3. 5.3 Properties with Only Getters
    4. 5.4 Private Fields
    5. 5.5 Auxiliary Constructors
    6. 5.6 The Primary Constructor
    7. 5.7 Nested Classes
    8. Exercises
  17. Chapter 6. Objects and Enumerations
    1. 6.1 Singletons
    2. 6.2 Companion Objects
    3. 6.3 Objects Extending a Class or Trait
    4. 6.4 The apply Method
    5. 6.5 Application Objects
    6. 6.6 Enumerations
    7. Exercises
  18. Chapter 7. Packages, Imports, and Exports
    1. 7.1 Packages
    2. 7.2 Package Scope Nesting
    3. 7.3 Chained Package Clauses
    4. 7.4 Top-of-File Notation
    5. 7.5 Package-Level Functions and Variables
    6. 7.6 Package Visibility
    7. 7.7 Imports
    8. 7.8 Imports Can Be Anywhere
    9. 7.9 Renaming and Hiding Members
    10. 7.10 Implicit Imports
    11. 7.11 Exports
    12. Exercises
  19. Chapter 8. Inheritance
    1. 8.1 Extending a Class
    2. 8.2 Overriding Methods
    3. 8.3 Type Checks and Casts
    4. 8.4 Superclass Construction
    5. 8.5 Anonymous Subclasses
    6. 8.6 Abstract Classes
    7. 8.7 Abstract Fields
    8. 8.8 Overriding Fields
    9. 8.9 Open and Sealed Classes
    10. 8.10 Protected Fields and Methods
    11. 8.11 Construction Order
    12. 8.12 The Scala Inheritance Hierarchy
    13. 8.13 Object Equality
    14. 8.14 Multiversal Equality
    15. 8.15 Value Classes
    16. Exercises
  20. Chapter 9. Files and Regular Expressions
    1. 9.1 Reading Lines
    2. 9.2 Reading Characters
    3. 9.3 Reading Tokens and Numbers
    4. 9.4 Reading from URLs and Other Sources
    5. 9.5 Writing Files
    6. 9.6 Visiting Directories
    7. 9.7 Serialization
    8. 9.8 Process Control
    9. 9.9 Regular Expressions
    10. 9.10 Regular Expression Groups
    11. Exercises
  21. Chapter 10. Traits
    1. 10.1 Why No Multiple Inheritance?
    2. 10.2 Traits as Interfaces
    3. 10.3 Traits with Concrete Methods
    4. 10.4 Traits for Rich Interfaces
    5. 10.5 Objects with Traits
    6. 10.6 Layered Traits
    7. 10.7 Overriding Abstract Methods in Traits
    8. 10.8 Concrete Fields in Traits
    9. 10.9 Abstract Fields in Traits
    10. 10.10 Trait Construction Order
    11. 10.11 Trait Constructors with Parameters
    12. 10.12 Traits Extending Classes
    13. 10.13 What Happens under the Hood
    14. 10.14 Transparent Traits
    15. 10.15 Self Types
    16. Exercises
  22. Chapter 11. Operators
    1. 11.1 Identifiers
    2. 11.2 Infix Operators
    3. 11.3 Unary Operators
    4. 11.4 Assignment Operators
    5. 11.5 Precedence
    6. 11.6 Associativity
    7. 11.7 The apply and update Methods
    8. 11.8 The unapply Method
    9. 11.9 The unapplySeq Method
    10. 11.10 Alternative Forms of the unapply and unapplySeq Methods
    11. 11.11 Dynamic Invocation
    12. 11.12 Typesafe Selection and Application
    13. Exercises
  23. Chapter 12. Higher-Order Functions
    1. 12.1 Functions as Values
    2. 12.2 Anonymous Functions
    3. 12.3 Parameters That Are Functions
    4. 12.4 Parameter Inference
    5. 12.5 Useful Higher-Order Functions
    6. 12.6 Closures
    7. 12.7 Interoperability with Lambda Expressions
    8. 12.8 Currying
    9. 12.9 Methods for Composing, Currying, and Tupling
    10. 12.10 Control Abstractions
    11. 12.11 Nonlocal Returns
    12. Exercises
  24. Chapter 13. Collections
    1. 13.1 The Main Collections Traits
    2. 13.2 Mutable and Immutable Collections
    3. 13.3 Sequences
    4. 13.4 Lists
    5. 13.5 Sets
    6. 13.6 Operators for Adding or Removing Elements
    7. 13.7 Common Methods
    8. 13.8 Mapping a Function
    9. 13.9 Reducing, Folding, and Scanning
    10. 13.10 Zipping
    11. 13.11 Iterators
    12. 13.12 Lazy Lists
    13. 13.13 Interoperability with Java Collections
    14. Exercises
  25. Chapter 14. Pattern Matching
    1. 14.1 A Better Switch
    2. 14.2 Guards
    3. 14.3 Variables in Patterns
    4. 14.4 Type Patterns
    5. 14.5 The Matchable Trait
    6. 14.6 Matching Arrays, Lists, and Tuples
    7. 14.7 Extractors
    8. 14.8 Patterns in Variable Declarations
    9. 14.9 Patterns in for Expressions
    10. 14.10 Case Classes
    11. 14.11 Matching Nested Structures
    12. 14.12 Sealed Classes
    13. 14.13 Parameterized Enumerations
    14. 14.14 Partial Functions
    15. 14.15 Infix Notation in case Clauses
    16. Exercises
  26. Chapter 15. Annotations
    1. 15.1 What Are Annotations?
    2. 15.2 Annotation Placement
    3. 15.3 Annotation Arguments
    4. 15.4 Annotations for Java Features
    5. 15.5 Annotations for Optimizations
    6. 15.6 Annotations for Errors and Warnings
    7. 15.7 Annotation Declarations
    8. Exercises
  27. Chapter 16. Futures
    1. 16.1 Running Tasks in the Future
    2. 16.2 Waiting for Results
    3. 16.3 The Try Class
    4. 16.4 Callbacks
    5. 16.5 Composing Future Tasks
    6. 16.6 Other Future Transformations
    7. 16.7 Methods in the Future Object
    8. 16.8 Promises
    9. 16.9 Execution Contexts
    10. Exercises
  28. Chapter 17. Type Parameters
    1. 17.1 Generic Classes
    2. 17.2 Generic Functions
    3. 17.3 Bounds for Type Variables
    4. 17.4 Context Bounds
    5. 17.5 The ClassTag Context Bound
    6. 17.6 Multiple Bounds
    7. 17.7 Type Constraints
    8. 17.8 Variance
    9. 17.9 Co- and Contravariant Positions
    10. 17.10 Objects Can’t Be Generic
    11. 17.11 Wildcards
    12. 17.12 Polymorphic Functions
    13. Exercises
  29. Chapter 18. Advanced Types
    1. 18.1 Union Types
    2. 18.2 Intersection Types
    3. 18.3 Type Aliases
    4. 18.4 Structural Types
    5. 18.5 Literal Types
    6. 18.6 The Singleton Type Operator
    7. 18.7 Abstract Types
    8. 18.8 Dependent Types
    9. 18.9 Abstract Type Bounds
    10. Exercises
  30. Chapter 19. Contextual Abstractions
    1. 19.1 Context Parameters
    2. 19.2 More about Context Parameters
    3. 19.3 Declaring Given Instances
    4. 19.4 Givens in for and match Expressions
    5. 19.5 Importing Givens
    6. 19.6 Extension Methods
    7. 19.7 Where Extension Methods Are Found
    8. 19.8 Implicit Conversions
    9. 19.9 Rules for Implicit Conversions
    10. 19.10 Importing Implicit Conversions
    11. 19.11 Context Functions
    12. 19.12 Evidence
    13. 19.13 The @implicitNotFound Annotation
    14. Exercises
  31. Chapter 20. Type-Level Programming
    1. 20.1 Match Types
    2. 20.2 Heterogeneous Lists
    3. 20.3 Literal Type Arithmetic
    4. 20.4 Inline Code
    5. 20.5 Type Classes
    6. 20.6 Mirrors
    7. 20.7 Type Class Derivation
    8. 20.8 Higher-Kinded Types
    9. 20.9 Type Lambdas
    10. 20.10 A Brief Introduction into Macros
    11. Exercises
  32. Index
  33. Code Snippets

Product information

  • Title: Scala for the Impatient, 3rd Edition
  • Author(s): Cay S. Horstmann
  • Release date: December 2022
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780138033613