Books & Videos

Table of Contents

  1. Chapter 1 Strings

    1. Introduction

    2. Testing String Equality

    3. Creating Multiline Strings

    4. Splitting Strings

    5. Substituting Variables into Strings

    6. Processing a String One Character at a Time

    7. Finding Patterns in Strings

    8. Replacing Patterns in Strings

    9. Extracting Parts of a String That Match Patterns

    10. Accessing a Character in a String

    11. Add Your Own Methods to the String Class

  2. Chapter 2 Numbers

    1. Introduction

    2. Parsing a Number from a String

    3. Converting Between Numeric Types (Casting)

    4. Overriding the Default Numeric Type

    5. Replacements for ++ and −−

    6. Comparing Floating-Point Numbers

    7. Handling Very Large Numbers

    8. Generating Random Numbers

    9. Creating a Range, List, or Array of Numbers

    10. Formatting Numbers and Currency

  3. Chapter 3 Control Structures

    1. Introduction

    2. Looping with for and foreach

    3. Using for Loops with Multiple Counters

    4. Using a for Loop with Embedded if Statements (Guards)

    5. Creating a for Comprehension (for/yield Combination)

    6. Implementing break and continue

    7. Using the if Construct Like a Ternary Operator

    8. Using a Match Expression Like a switch Statement

    9. Matching Multiple Conditions with One Case Statement

    10. Assigning the Result of a Match Expression to a Variable

    11. Accessing the Value of the Default Case in a Match Expression

    12. Using Pattern Matching in Match Expressions

    13. Using Case Classes in Match Expressions

    14. Adding if Expressions (Guards) to Case Statements

    15. Using a Match Expression Instead of isInstanceOf

    16. Working with a List in a Match Expression

    17. Matching One or More Exceptions with try/catch

    18. Declaring a Variable Before Using It in a try/catch/finally Block

    19. Creating Your Own Control Structures

  4. Chapter 4 Classes and Properties

    1. Introduction

    2. Creating a Primary Constructor

    3. Controlling the Visibility of Constructor Fields

    4. Defining Auxiliary Constructors

    5. Defining a Private Primary Constructor

    6. Providing Default Values for Constructor Parameters

    7. Overriding Default Accessors and Mutators

    8. Preventing Getter and Setter Methods from Being Generated

    9. Assigning a Field to a Block or Function

    10. Setting Uninitialized var Field Types

    11. Handling Constructor Parameters When Extending a Class

    12. Calling a Superclass Constructor

    13. When to Use an Abstract Class

    14. Defining Properties in an Abstract Base Class (or Trait)

    15. Generating Boilerplate Code with Case Classes

    16. Defining an equals Method (Object Equality)

    17. Creating Inner Classes

  5. Chapter 5 Methods

    1. Introduction

    2. Controlling Method Scope

    3. Calling a Method on a Superclass

    4. Setting Default Values for Method Parameters

    5. Using Parameter Names When Calling a Method

    6. Defining a Method That Returns Multiple Items (Tuples)

    7. Forcing Callers to Leave Parentheses off Accessor Methods

    8. Creating Methods That Take Variable-Argument Fields

    9. Declaring That a Method Can Throw an Exception

    10. Supporting a Fluent Style of Programming

  6. Chapter 6 Objects

    1. Introduction

    2. Object Casting

    3. The Scala Equivalent of Java’s .class

    4. Determining the Class of an Object

    5. Launching an Application with an Object

    6. Creating Singletons with object

    7. Creating Static Members with Companion Objects

    8. Putting Common Code in Package Objects

    9. Creating Object Instances Without Using the new Keyword

    10. Implement the Factory Method in Scala with apply

  7. Chapter 7 Packaging and Imports

    1. Introduction

    2. Packaging with the Curly Braces Style Notation

    3. Importing One or More Members

    4. Renaming Members on Import

    5. Hiding a Class During the Import Process

    6. Using Static Imports

    7. Using Import Statements Anywhere

  8. Chapter 8 Traits

    1. Introduction

    2. Using a Trait as an Interface

    3. Using Abstract and Concrete Fields in Traits

    4. Using a Trait Like an Abstract Class

    5. Using Traits as Simple Mixins

    6. Limiting Which Classes Can Use a Trait by Inheritance

    7. Marking Traits So They Can Only Be Used by Subclasses of a Certain Type

    8. Ensuring a Trait Can Only Be Added to a Type That Has a Specific Method

    9. Adding a Trait to an Object Instance

    10. Extending a Java Interface Like a Trait

  9. Chapter 9 Functional Programming

    1. Introduction

    2. Using Function Literals (Anonymous Functions)

    3. Using Functions as Variables

    4. Defining a Method That Accepts a Simple Function Parameter

    5. More Complex Functions

    6. Using Closures

    7. Using Partially Applied Functions

    8. Creating a Function That Returns a Function

    9. Creating Partial Functions

    10. A Real-World Example

  10. Chapter 10 Collections

    1. Introduction

    2. Understanding the Collections Hierarchy

    3. Choosing a Collection Class

    4. Choosing a Collection Method to Solve a Problem

    5. Understanding the Performance of Collections

    6. Declaring a Type When Creating a Collection

    7. Understanding Mutable Variables with Immutable Collections

    8. Make Vector Your “Go To” Immutable Sequence

    9. Make ArrayBuffer Your “Go To” Mutable Sequence

    10. Looping over a Collection with foreach

    11. Looping over a Collection with a for Loop

    12. Using zipWithIndex or zip to Create Loop Counters

    13. Using Iterators

    14. Transforming One Collection to Another with for/yield

    15. Transforming One Collection to Another with map

    16. Flattening a List of Lists with flatten

    17. Combining map and flatten with flatMap

    18. Using filter to Filter a Collection

    19. Extracting a Sequence of Elements from a Collection

    20. Splitting Sequences into Subsets (groupBy, partition, etc.)

    21. Walking Through a Collection with the reduce and fold Methods

    22. Extracting Unique Elements from a Sequence

    23. Merging Sequential Collections

    24. Merging Two Sequential Collections into Pairs with zip

    25. Creating a Lazy View on a Collection

    26. Populating a Collection with a Range

    27. Creating and Using Enumerations

    28. Tuples, for When You Just Need a Bag of Things

    29. Sorting a Collection

    30. Converting a Collection to a String with mkString

  11. Chapter 11 List, Array, Map, Set (and More)

    1. Introduction

    2. Different Ways to Create and Populate a List

    3. Creating a Mutable List

    4. Adding Elements to a List

    5. Deleting Elements from a List (or ListBuffer)

    6. Merging (Concatenating) Lists

    7. Using Stream, a Lazy Version of a List

    8. Different Ways to Create and Update an Array

    9. Creating an Array Whose Size Can Change (ArrayBuffer)

    10. Deleting Array and ArrayBuffer Elements

    11. Sorting Arrays

    12. Creating Multidimensional Arrays

    13. Creating Maps

    14. Choosing a Map Implementation

    15. Adding, Updating, and Removing Elements with a Mutable Map

    16. Adding, Updating, and Removing Elements with Immutable Maps

    17. Accessing Map Values

    18. Traversing a Map

    19. Getting the Keys or Values from a Map

    20. Reversing Keys and Values

    21. Testing for the Existence of a Key or Value in a Map

    22. Filtering a Map

    23. Sorting an Existing Map by Key or Value

    24. Finding the Largest Key or Value in a Map

    25. Adding Elements to a Set

    26. Deleting Elements from Sets

    27. Using Sortable Sets

    28. Using a Queue

    29. Using a Stack

    30. Using a Range

  12. Chapter 12 Files and Processes

    1. Introduction

    2. How to Open and Read a Text File

    3. Writing Text Files

    4. Reading and Writing Binary Files

    5. How to Process Every Character in a Text File

    6. How to Process a CSV File

    7. Pretending that a String Is a File

    8. Using Serialization

    9. Listing Files in a Directory

    10. Listing Subdirectories Beneath a Directory

    11. Executing External Commands

    12. Executing External Commands and Using STDOUT

    13. Handling STDOUT and STDERR for External Commands

    14. Building a Pipeline of Commands

    15. Redirecting the STDOUT and STDIN of External Commands

    16. Using AND (&&) and OR (||) with Processes

    17. Handling Wildcard Characters in External Commands

    18. How to Run a Process in a Different Directory

    19. Setting Environment Variables When Running Commands

    20. An Index of Methods to Execute External Commands

  13. Chapter 13 Actors and Concurrency

    1. Introduction

    2. Getting Started with a Simple Actor

    3. Creating an Actor Whose Class Constructor Requires Arguments

    4. How to Communicate Between Actors

    5. Understanding the Methods in the Akka Actor Lifecycle

    6. Starting an Actor

    7. Stopping Actors

    8. Shutting Down the Akka Actor System

    9. Monitoring the Death of an Actor with watch

    10. Simple Concurrency with Futures

    11. Sending a Message to an Actor and Waiting for a Reply

    12. Switching Between Different States with become

    13. Using Parallel Collections

  14. Chapter 14 Command-Line Tasks

    1. Introduction

    2. Getting Started with the Scala REPL

    3. Pasting and Loading Blocks of Code into the REPL

    4. Adding JAR Files and Classes to the REPL Classpath

    5. Running a Shell Command from the REPL

    6. Compiling with scalac and Running with scala

    7. Disassembling and Decompiling Scala Code

    8. Finding Scala Libraries

    9. Generating Documentation with scaladoc

    10. Faster Command-Line Compiling with fsc

    11. Using Scala as a Scripting Language

    12. Accessing Command-Line Arguments from a Script

    13. Prompting for Input from a Scala Shell Script

    14. Make Your Scala Scripts Run Faster

  15. Chapter 15 Web Services

    1. Introduction

    2. Creating a JSON String from a Scala Object

    3. Creating a JSON String from Classes That Have Collections

    4. Creating a Simple Scala Object from a JSON String

    5. Parsing JSON Data into an Array of Objects

    6. Creating Web Services with Scalatra

    7. Replacing XML Servlet Mappings with Scalatra Mounts

    8. Accessing Scalatra Web Service GET Parameters

    9. Accessing POST Request Data with Scalatra

    10. Creating a Simple GET Request Client

    11. Sending JSON Data to a POST URL

    12. Getting URL Headers

    13. Setting URL Headers When Sending a Request

    14. Creating a GET Request Web Service with the Play Framework

    15. POSTing JSON Data to a Play Framework Web Service

  16. Chapter 16 Databases and Persistence

    1. Introduction

    2. Connecting to MySQL with JDBC

    3. Connecting to a Database with the Spring Framework

    4. Connecting to MongoDB and Inserting Data

    5. Inserting Documents into MongoDB with insert, save, or +=

    6. Searching a MongoDB Collection

    7. Updating Documents in a MongoDB Collection

    8. Accessing the MongoDB Document ID Field

    9. Deleting Documents in a MongoDB Collection

    10. A Quick Look at Slick

  17. Chapter 17 Interacting with Java

    1. Introduction

    2. Going to and from Java Collections

    3. Add Exception Annotations to Scala Methods to Work with Java

    4. Using @SerialVersionUID and Other Annotations

    5. Using the Spring Framework

    6. Annotating varargs Methods

    7. When Java Code Requires JavaBeans

    8. Wrapping Traits with Implementations

  18. Chapter 18 The Simple Build Tool (SBT)

    1. Introduction

    2. Creating a Project Directory Structure for SBT

    3. Compiling, Running, and Packaging a Scala Project with SBT

    4. Running Tests with SBT and ScalaTest

    5. Managing Dependencies with SBT

    6. Controlling Which Version of a Managed Dependency Is Used

    7. Creating a Project with Subprojects

    8. Using SBT with Eclipse

    9. Generating Project API Documentation

    10. Specifying a Main Class to Run

    11. Using GitHub Projects as Project Dependencies

    12. Telling SBT How to Find a Repository (Working with Resolvers)

    13. Resolving Problems by Getting an SBT Stack Trace

    14. Setting the SBT Log Level

    15. Deploying a Single, Executable JAR File

    16. Publishing Your Library

    17. Using Build.scala Instead of build.sbt

    18. Using a Maven Repository Library with SBT

    19. Building a Scala Project with Ant

  19. Chapter 19 Types

    1. Introduction

    2. Creating Classes That Use Generic Types

    3. Creating a Method That Takes a Simple Generic Type

    4. Using Duck Typing (Structural Types)

    5. Make Mutable Collections Invariant

    6. Make Immutable Collections Covariant

    7. Create a Collection Whose Elements Are All of Some Base Type

    8. Selectively Adding New Behavior to a Closed Model

    9. Building Functionality with Types

  20. Chapter 20 Idioms

    1. Introduction

    2. Create Methods with No Side Effects (Pure Functions)

    3. Prefer Immutable Objects

    4. Think “Expression-Oriented Programming”

    5. Use Match Expressions and Pattern Matching

    6. Eliminate null Values from Your Code

    7. Using the Option/Some/None Pattern

  1. Colophon