You are previewing Scala Cookbook.
O'Reilly logo
Scala Cookbook

Book Description

Save time and trouble when using Scala to build object-oriented, functional, and concurrent applications. With more than 250 ready-to-use recipes and 700 code examples, this comprehensive cookbook covers the most common problems you’ll encounter when using the Scala language, libraries, and tools.

Table of Contents

  1. Scala Cookbook
  2. Dedication
  3. Preface
    1. 1. The Scala Language
      1. Solutions
      2. Audience
      3. Contents of This Book
      4. Online Bonus Chapters
      5. Installing the Software
      6. How the Code Listings Work
      7. Conventions Used in This Book
      8. Using Code Examples
      9. Safari® Books Online
      10. How to Contact Us
      11. Acknowledgments
  4. 1. Strings
    1. Introduction
      1. Add Methods to Closed Classes
    2. 1.1. Testing String Equality
      1. Problem
      2. Solution
      3. Discussion
    3. 1.2. Creating Multiline Strings
      1. Problem
      2. Solution
      3. Discussion
    4. 1.3. Splitting Strings
      1. Problem
      2. Solution
      3. Discussion
        1. About that split method...
    5. 1.4. Substituting Variables into Strings
      1. Problem
      2. Solution
        1. Using expressions in string literals
        2. s is a method
        3. The f string interpolator (printf style formatting)
        4. The raw interpolator
        5. Create your own interpolator
      3. Discussion
      4. See Also
    6. 1.5. Processing a String One Character at a Time
      1. Problem
      2. Solution
      3. Discussion
        1. Understanding how map works
        2. A complete example
      4. See Also
    7. 1.6. Finding Patterns in Strings
      1. Problem
      2. Solution
      3. Discussion
        1. Handling the Option returned by findFirstIn
      4. See Also
    8. 1.7. Replacing Patterns in Strings
      1. Problem
      2. Solution
      3. See Also
    9. 1.8. Extracting Parts of a String That Match Patterns
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 1.9. Accessing a Character in a String
      1. Problem
      2. Solution
      3. Discussion
    11. 1.10. Add Your Own Methods to the String Class
      1. Problem
      2. Solution
        1. Put the implicit class in an object
        2. Put the implicit class in a package object
        3. Using versions of Scala prior to version 2.10
      3. Discussion
        1. Annotate your method return type
        2. Returning other types
  5. 2. Numbers
    1. Introduction
      1. Complex Numbers and Dates
    2. 2.1. Parsing a Number from a String
      1. Problem
      2. Solution
        1. Handling a base and radix
      3. Discussion
        1. Alternatives to Option
      4. See Also
    3. 2.2. Converting Between Numeric Types (Casting)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 2.3. Overriding the Default Numeric Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 2.4. Replacements for ++ and −−
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 2.5. Comparing Floating-Point Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 2.6. Handling Very Large Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 2.7. Generating Random Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 2.8. Creating a Range, List, or Array of Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 2.9. Formatting Numbers and Currency
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  6. 3. Control Structures
    1. Introduction
    2. 3.1. Looping with for and foreach
      1. Problem
      2. Solution
        1. Returning values from a for loop
        2. for loop counters
        3. Generators and guards
        4. Looping over a Map
      3. Discussion
        1. How for loops are translated
      4. See Also
    3. 3.2. Using for Loops with Multiple Counters
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 3.3. Using a for Loop with Embedded if Statements (Guards)
      1. Problem
      2. Solution
      3. Discussion
    5. 3.4. Creating a for Comprehension (for/yield Combination)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 3.5. Implementing break and continue
      1. Problem
      2. Solution
        1. The break example
        2. The continue example
        3. General syntax
        4. About that continue example...
        5. Nested loops and labeled breaks
      3. Discussion
      4. See Also
    7. 3.6. Using the if Construct Like a Ternary Operator
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 3.7. Using a Match Expression Like a switch Statement
      1. Problem
      2. Solution
        1. The @switch annotation
      3. Discussion
        1. Handling the default case
        2. Do you really need a switch statement?
      4. See Also
    9. 3.8. Matching Multiple Conditions with One Case Statement
      1. Problem
      2. Solution
      3. See Also
    10. 3.9. Assigning the Result of a Match Expression to a Variable
      1. Problem
      2. Solution
      3. See Also
    11. 3.10. Accessing the Value of the Default Case in a Match Expression
      1. Problem
      2. Solution
      3. Discussion
    12. 3.11. Using Pattern Matching in Match Expressions
      1. Problem
      2. Solution
      3. Discussion
        1. Patterns
        2. Adding variables to patterns
        3. Using Some and None in match expressions
      4. See Also
    13. 3.12. Using Case Classes in Match Expressions
      1. Problem
      2. Solution
    14. 3.13. Adding if Expressions (Guards) to Case Statements
      1. Problem
      2. Solution
      3. Discussion
    15. 3.14. Using a Match Expression Instead of isInstanceOf
      1. Problem
      2. Solution
      3. Discussion
    16. 3.15. Working with a List in a Match Expression
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. 3.16. Matching One or More Exceptions with try/catch
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 3.17. Declaring a Variable Before Using It in a try/catch/finally Block
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. 3.18. Creating Your Own Control Structures
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  7. 4. Classes and Properties
    1. Introduction
    2. 4.1. Creating a Primary Constructor
      1. Problem
      2. Solution
      3. Discussion
        1. A comparison with Java
        2. Those _$eq methods
        3. Summary
    3. 4.2. Controlling the Visibility of Constructor Fields
      1. Problem
      2. Solution
        1. var fields
        2. val fields
        3. Fields without val or var
        4. Adding private to val or var
      3. Discussion
        1. Case classes
    4. 4.3. Defining Auxiliary Constructors
      1. Problem
      2. Solution
      3. Discussion
        1. Generating auxiliary constructors for case classes
      4. See Also
    5. 4.4. Defining a Private Primary Constructor
      1. Problem
      2. Solution
      3. Discussion
        1. Utility classes
    6. 4.5. Providing Default Values for Constructor Parameters
      1. Problem
      2. Solution
      3. Discussion
        1. Multiple parameters
        2. Using named parameters
      4. See Also
    7. 4.6. Overriding Default Accessors and Mutators
      1. Problem
      2. Solution
      3. Discussion
        1. Summary
    8. 4.7. Preventing Getter and Setter Methods from Being Generated
      1. Problem
      2. Solution
      3. Discussion
        1. Object-private fields
    9. 4.8. Assigning a Field to a Block or Function
      1. Problem
      2. Solution
      3. Discussion
    10. 4.9. Setting Uninitialized var Field Types
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 4.10. Handling Constructor Parameters When Extending a Class
      1. Problem
      2. Solution
      3. Discussion
    12. 4.11. Calling a Superclass Constructor
      1. Problem
      2. Solution
        1. Auxiliary constructors
    13. 4.12. When to Use an Abstract Class
      1. Problem
      2. Solution
      3. Discussion
    14. 4.13. Defining Properties in an Abstract Base Class (or Trait)
      1. Problem
      2. Solution
        1. Abstract val and var fields
      3. Discussion
        1. Concrete val fields in abstract classes
        2. Concrete var fields in abstract classes
        3. Don’t use null
      4. See Also
    15. 4.14. Generating Boilerplate Code with Case Classes
      1. Problem
      2. Solution
      3. Discussion
        1. Generated code
      4. See Also
    16. 4.15. Defining an equals Method (Object Equality)
      1. Problem
      2. Solution
      3. Discussion
        1. Theory
      4. See Also
    17. 4.16. Creating Inner Classes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  8. 5. Methods
    1. Introduction
    2. 5.1. Controlling Method Scope
      1. Problem
      2. Solution
        1. Object-private scope
        2. Private scope
        3. Protected scope
        4. Package scope
        5. More package-level control
        6. Public scope
      3. Discussion
    3. 5.2. Calling a Method on a Superclass
      1. Problem
      2. Solution
        1. Controlling which trait you call a method from
    4. 5.3. Setting Default Values for Method Parameters
      1. Problem
      2. Solution
      3. Discussion
    5. 5.4. Using Parameter Names When Calling a Method
      1. Problem
      2. Solution
      3. Discussion
    6. 5.5. Defining a Method That Returns Multiple Items (Tuples)
      1. Problem
      2. Solution
      3. Discussion
        1. Working with tuples
      4. See Also
    7. 5.6. Forcing Callers to Leave Parentheses off Accessor Methods
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 5.7. Creating Methods That Take Variable-Argument Fields
      1. Problem
      2. Solution
        1. Use _* to adapt a sequence
      3. Discussion
    9. 5.8. Declaring That a Method Can Throw an Exception
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 5.9. Supporting a Fluent Style of Programming
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  9. 6. Objects
    1. Introduction
    2. 6.1. Object Casting
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 6.2. The Scala Equivalent of Java’s .class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 6.3. Determining the Class of an Object
      1. Problem
      2. Solution
      3. Discussion
    5. 6.4. Launching an Application with an Object
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 6.5. Creating Singletons with object
      1. Problem
      2. Solution
      3. Discussion
    7. 6.6. Creating Static Members with Companion Objects
      1. Problem
      2. Solution
      3. Discussion
        1. Accessing private members
    8. 6.7. Putting Common Code in Package Objects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 6.8. Creating Object Instances Without Using the new Keyword
      1. Problem
      2. Solution
        1. Creating a companion object with an apply method
        2. Declare your class as a case class
      3. Discussion
        1. Providing multiple constructors with additional apply methods
        2. Providing multiple constructors for case classes
    10. 6.9. Implement the Factory Method in Scala with apply
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  10. 7. Packaging and Imports
    1. Introduction
    2. 7.1. Packaging with the Curly Braces Style Notation
      1. Problem
      2. Solution
      3. Discussion
    3. 7.2. Importing One or More Members
      1. Problem
      2. Solution
      3. Discussion
        1. Placing import statements anywhere
    4. 7.3. Renaming Members on Import
      1. Problem
      2. Solution
      3. Discussion
    5. 7.4. Hiding a Class During the Import Process
      1. Problem
      2. Solution
    6. 7.5. Using Static Imports
      1. Problem
      2. Solution
      3. Discussion
    7. 7.6. Using Import Statements Anywhere
      1. Problem
      2. Solution
      3. Discussion
  11. 8. Traits
    1. Introduction
    2. 8.1. Using a Trait as an Interface
      1. Problem
      2. Solution
      3. Discussion
    3. 8.2. Using Abstract and Concrete Fields in Traits
      1. Problem
      2. Solution
      3. Discussion
    4. 8.3. Using a Trait Like an Abstract Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 8.4. Using Traits as Simple Mixins
      1. Problem
      2. Solution
      3. See Also
    6. 8.5. Limiting Which Classes Can Use a Trait by Inheritance
      1. Problem
      2. Solution
      3. Discussion
      4. Discussion
      5. See Also
    7. 8.6. Marking Traits So They Can Only Be Used by Subclasses of a Certain Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 8.7. Ensuring a Trait Can Only Be Added to a Type That Has a Specific Method
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 8.8. Adding a Trait to an Object Instance
      1. Problem
      2. Solution
      3. Discussion
    10. 8.9. Extending a Java Interface Like a Trait
      1. Problem
      2. Solution
  12. 9. Functional Programming
    1. Introduction
    2. 9.1. Using Function Literals (Anonymous Functions)
      1. Problem
      2. Solution
      3. Discussion
    3. 9.2. Using Functions as Variables
      1. Problem
      2. Solution
      3. Discussion
        1. Using a method like an anonymous function
        2. Assigning an existing function/method to a function variable
      4. See Also
    4. 9.3. Defining a Method That Accepts a Simple Function Parameter
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 9.4. More Complex Functions
      1. Problem
      2. Solution
      3. Discussion
        1. Passing in a function with other parameters
    6. 9.5. Using Closures
      1. Problem
      2. Solution
      3. Discussion
        1. A second example
        2. Using closures with other data types
        3. A comparison to Java
      4. See Also
    7. 9.6. Using Partially Applied Functions
      1. Problem
      2. Solution
      3. Discussion
    8. 9.7. Creating a Function That Returns a Function
      1. Problem
      2. Solution
      3. Discussion
        1. Another example
      4. See Also
    9. 9.8. Creating Partial Functions
      1. Problem
      2. Solution
        1. The PartialFunction explained
        2. orElse and andThen
      3. Discussion
      4. See Also
    10. 9.9. A Real-World Example
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  13. 10. Collections
    1. Introduction
      1. A Few Important Concepts
    2. 10.1. Understanding the Collections Hierarchy
      1. Problem
      2. Solution
        1. Sequences
        2. Maps
        3. Sets
        4. More collection classes
        5. Strict and lazy collections
    3. 10.2. Choosing a Collection Class
      1. Problem
      2. Solution
        1. Choosing a sequence
        2. Choosing a map
        3. Choosing a set
        4. Types that act like collections
        5. Strict and lazy collections
      3. See Also
    4. 10.3. Choosing a Collection Method to Solve a Problem
      1. Problem
      2. Solution
        1. Methods organized by category
        2. Common collection methods
        3. Mutable collection methods
        4. Immutable collection operators
        5. Maps
      3. Discussion
    5. 10.4. Understanding the Performance of Collections
      1. Problem
      2. Solution
        1. Map and set performance characteristics
      3. See Also
    6. 10.5. Declaring a Type When Creating a Collection
      1. Problem
      2. Solution
      3. Discussion
    7. 10.6. Understanding Mutable Variables with Immutable Collections
      1. Problem
      2. Solution
        1. Summary
      3. See Also
    8. 10.7. Make Vector Your “Go To” Immutable Sequence
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 10.8. Make ArrayBuffer Your “Go To” Mutable Sequence
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 10.9. Looping over a Collection with foreach
      1. Problem
      2. Solution
      3. Discussion
    11. 10.10. Looping over a Collection with a for Loop
      1. Problem
      2. Solution
        1. The for/yield construct
        2. Maps
      3. Discussion
      4. See Also
    12. 10.11. Using zipWithIndex or zip to Create Loop Counters
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 10.12. Using Iterators
      1. Problem
      2. Solution
      3. See Also
    14. 10.13. Transforming One Collection to Another with for/yield
      1. Problem
      2. Solution
      3. Discussion
        1. Using guards
      4. See Also
    15. 10.14. Transforming One Collection to Another with map
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 10.15. Flattening a List of Lists with flatten
      1. Problem
      2. Solution
    17. 10.16. Combining map and flatten with flatMap
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 10.17. Using filter to Filter a Collection
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. 10.18. Extracting a Sequence of Elements from a Collection
      1. Problem
      2. Solution
        1. Even more methods
    20. 10.19. Splitting Sequences into Subsets (groupBy, partition, etc.)
      1. Problem
      2. Solution
    21. 10.20. Walking Through a Collection with the reduce and fold Methods
      1. Problem
      2. Solution
        1. Show each step in the process
        2. Working with other sequences and types
        3. foldLeft, reduceRight, and foldRight
        4. The difference between reduceLeft and reduceRight
        5. scanLeft and scanRight
    22. 10.21. Extracting Unique Elements from a Sequence
      1. Problem
      2. Solution
        1. Using distinct with your own classes
      3. See Also
    23. 10.22. Merging Sequential Collections
      1. Problem
      2. Solution
      3. Discussion
    24. 10.23. Merging Two Sequential Collections into Pairs with zip
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    25. 10.24. Creating a Lazy View on a Collection
      1. Problem
      2. Solution
      3. Discussion
        1. Use cases
      4. See Also
    26. 10.25. Populating a Collection with a Range
      1. Problem
      2. Solution
      3. Discussion
    27. 10.26. Creating and Using Enumerations
      1. Problem
      2. Solution
      3. See Also
    28. 10.27. Tuples, for When You Just Need a Bag of Things
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    29. 10.28. Sorting a Collection
      1. Problem
      2. Solution
      3. Discussion
        1. Mix in the Ordered trait
      4. See Also
    30. 10.29. Converting a Collection to a String with mkString
      1. Problem
      2. Solution
      3. Discussion
  14. 11. List, Array, Map, Set (and More)
    1. Introduction
      1. List
      2. Array (and ArrayBuffer)
      3. Maps
      4. Sets
    2. 11.1. Different Ways to Create and Populate a List
      1. Problem
      2. Solution
      3. See Also
    3. 11.2. Creating a Mutable List
      1. Problem
      2. Solution
      3. Discussion
    4. 11.3. Adding Elements to a List
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 11.4. Deleting Elements from a List (or ListBuffer)
      1. Problem
      2. Solution
        1. ListBuffer
      3. Discussion
      4. See Also
    6. 11.5. Merging (Concatenating) Lists
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 11.6. Using Stream, a Lazy Version of a List
      1. Problem
      2. Solution
      3. See Also
    8. 11.7. Different Ways to Create and Update an Array
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 11.8. Creating an Array Whose Size Can Change (ArrayBuffer)
      1. Problem
      2. Solution
    10. 11.9. Deleting Array and ArrayBuffer Elements
      1. Problem
      2. Solution
        1. Array
    11. 11.10. Sorting Arrays
      1. Problem
      2. Solution
        1. Other solutions
      3. See Also
    12. 11.11. Creating Multidimensional Arrays
      1. Problem
      2. Solution
        1. Using Array.ofDim
        2. Using an array of arrays
      3. Discussion
    13. 11.12. Creating Maps
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. 11.13. Choosing a Map Implementation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. 11.14. Adding, Updating, and Removing Elements with a Mutable Map
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 11.15. Adding, Updating, and Removing Elements with Immutable Maps
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. 11.16. Accessing Map Values
      1. Problem
      2. Solution
      3. See Also
    18. 11.17. Traversing a Map
      1. Problem
      2. Solution
        1. Operating on map values
    19. 11.18. Getting the Keys or Values from a Map
      1. Problem
      2. Solution
    20. 11.19. Reversing Keys and Values
      1. Problem
      2. Solution
      3. See Also
    21. 11.20. Testing for the Existence of a Key or Value in a Map
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. 11.21. Filtering a Map
      1. Problem
      2. Solution
        1. Mutable maps
        2. Mutable and immutable maps
    23. 11.22. Sorting an Existing Map by Key or Value
      1. Problem
      2. Solution
      3. Discussion
        1. About that _*
      4. See Also
    24. 11.23. Finding the Largest Key or Value in a Map
      1. Problem
      2. Solution
      3. See Also
    25. 11.24. Adding Elements to a Set
      1. Problem
      2. Solution
        1. Mutable set
        2. Immutable set
    26. 11.25. Deleting Elements from Sets
      1. Problem
      2. Solution
        1. Mutable set
        2. Immutable set
    27. 11.26. Using Sortable Sets
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    28. 11.27. Using a Queue
      1. Problem
      2. Solution
      3. See Also
    29. 11.28. Using a Stack
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    30. 11.29. Using a Range
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  15. 12. Files and Processes
    1. 12.0. Introduction
    2. 12.1. How to Open and Read a Text File
      1. Problem
      2. Solution
        1. Using the concise syntax
        2. Properly closing the file
      3. Discussion
        1. Leaving files open
        2. Automatically closing the resource
        3. Handling exceptions
        4. Multiple fromFile methods
      4. See Also
    3. 12.2. Writing Text Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 12.3. Reading and Writing Binary Files
      1. Problem
      2. Solution
      3. See Also
    5. 12.4. How to Process Every Character in a Text File
      1. Problem
      2. Solution
    6. 12.5. How to Process a CSV File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 12.6. Pretending that a String Is a File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 12.7. Using Serialization
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 12.8. Listing Files in a Directory
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 12.9. Listing Subdirectories Beneath a Directory
      1. Problem
      2. Solution
      3. Discussion
    11. 12.10. Executing External Commands
      1. Problem
      2. Solution
      3. Discussion
        1. Using the lines method
        2. External commands versus built-in commands
    12. 12.11. Executing External Commands and Using STDOUT
      1. Problem
      2. Solution
      3. Discussion
        1. Unexpected newline characters
        2. Using the lines_! method
    13. 12.12. Handling STDOUT and STDERR for External Commands
      1. Problem
      2. Solution
      3. See Also
    14. 12.13. Building a Pipeline of Commands
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. 12.14. Redirecting the STDOUT and STDIN of External Commands
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 12.15. Using AND (&&) and OR (||) with Processes
      1. Problem
      2. Solution
    17. 12.16. Handling Wildcard Characters in External Commands
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 12.17. How to Run a Process in a Different Directory
      1. Problem
      2. Solution
    19. 12.18. Setting Environment Variables When Running Commands
      1. Problem
      2. Solution
      3. See Also
    20. 12.19. An Index of Methods to Execute External Commands
  16. 13. Actors and Concurrency
    1. Introduction
      1. The Actor Model
      2. Other Features
    2. 13.1. Getting Started with a Simple Actor
      1. Problem
      2. Solution
      3. Discussion
        1. Details
        2. ActorSystem
        3. ActorRef
      4. See Also
    3. 13.2. Creating an Actor Whose Class Constructor Requires Arguments
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 13.3. How to Communicate Between Actors
      1. Problem
      2. Solution
      3. Discussion
    5. 13.4. Understanding the Methods in the Akka Actor Lifecycle
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 13.5. Starting an Actor
      1. Problem
      2. Solution
      3. Discussion
    7. 13.6. Stopping Actors
      1. Problem
      2. Solution
      3. Discussion
        1. system.stop and context.stop
        2. PoisonPill message
        3. gracefulStop
        4. “Killing” an actor
      4. See Also
    8. 13.7. Shutting Down the Akka Actor System
      1. Problem
      2. Solution
      3. Discussion
    9. 13.8. Monitoring the Death of an Actor with watch
      1. Problem
      2. Solution
      3. Discussion
        1. Looking up actors
    10. 13.9. Simple Concurrency with Futures
      1. Problem
      2. Solution
        1. Run one task, but block
        2. Run one thing, but don’t block—use callback
        3. The onSuccess and onFailure callback methods
        4. Creating a method to return a Future[T]
        5. Run multiple things; something depends on them; join them together
      3. Discussion
        1. A future and ExecutionContext
        2. Callback methods
        3. For comprehensions (combinators: map, flatMap, filter, foreach, recoverWith, fallbackTo, andThen)
      4. See Also
    11. 13.10. Sending a Message to an Actor and Waiting for a Reply
      1. Problem
      2. Solution
      3. Discussion
    12. 13.11. Switching Between Different States with become
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 13.12. Using Parallel Collections
      1. Problem
      2. Solution
      3. Discussion
        1. Where are parallel collections useful?
        2. Performance
      4. See Also
  17. 14. Command-Line Tasks
    1. 14.0. Introduction
    2. 14.1. Getting Started with the Scala REPL
      1. Problem
      2. Solution
      3. Discussion
        1. REPL command-line options
        2. Deprecation and feature warnings
        3. The Scala Worksheet
      4. See Also
    3. 14.2. Pasting and Loading Blocks of Code into the REPL
      1. Problem
      2. Solution
        1. The :paste command
        2. The :load command
      3. Discussion
        1. Scala’s -i option
      4. See Also
    4. 14.3. Adding JAR Files and Classes to the REPL Classpath
      1. Problem
      2. Solution
    5. 14.4. Running a Shell Command from the REPL
      1. Problem
      2. Solution
        1. Scala’s -i option
      3. See Also
    6. 14.5. Compiling with scalac and Running with scala
      1. Problem
      2. Solution
      3. Discussion
    7. 14.6. Disassembling and Decompiling Scala Code
      1. Problem
      2. Solution
        1. Using javap
        2. Using scalac print options
        3. Use a decompiler
      3. Discussion
      4. See Also
    8. 14.7. Finding Scala Libraries
      1. Problem
      2. Solution
    9. 14.8. Generating Documentation with scaladoc
      1. Problem
      2. Solution
      3. Discussion
        1. Generating Scaladoc documentation with SBT
      4. See Also
    10. 14.9. Faster Command-Line Compiling with fsc
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 14.10. Using Scala as a Scripting Language
      1. Problem
      2. Solution
      3. Discussion
        1. Using the App trait or main method
        2. Building the classpath
      4. See Also
    12. 14.11. Accessing Command-Line Arguments from a Script
      1. Problem
      2. Solution
      3. Discussion
    13. 14.12. Prompting for Input from a Scala Shell Script
      1. Problem
      2. Solution
      3. Discussion
        1. Reading multiple values from one line
        2. Fun with output
      4. See Also
    14. 14.13. Make Your Scala Scripts Run Faster
      1. Problem
      2. Solution
      3. Discussion
  18. 15. Web Services
    1. Introduction
    2. 15.1. Creating a JSON String from a Scala Object
      1. Problem
      2. Solution
        1. Lift-JSON solution
        2. Gson solution
      3. Discussion
      4. See Also
    3. 15.2. Creating a JSON String from Classes That Have Collections
      1. Problem
      2. Solution
        1. Lift-JSON version 1
        2. Lift-JSON Version 2
      3. Discussion
      4. See Also
    4. 15.3. Creating a Simple Scala Object from a JSON String
      1. Problem
      2. Solution
      3. See Also
    5. 15.4. Parsing JSON Data into an Array of Objects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 15.5. Creating Web Services with Scalatra
      1. Problem
      2. Solution
        1. Add a new service
      3. Discussion
      4. See Also
    7. 15.6. Replacing XML Servlet Mappings with Scalatra Mounts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 15.7. Accessing Scalatra Web Service GET Parameters
      1. Problem
      2. Solution
    9. 15.8. Accessing POST Request Data with Scalatra
      1. Problem
      2. Solution
      3. Discussion
        1. Test the POST method with Scala code
        2. Test the POST method with a curl command
        3. Notes
    10. 15.9. Creating a Simple GET Request Client
      1. Problem
      2. Solution
        1. A simple use of scala.io.Source.fromURL
        2. Setting the timeout while using scala.io.Source.fromURL
        3. Using the Apache HttpClient
      3. Discussion
      4. See Also
    11. 15.10. Sending JSON Data to a POST URL
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 15.11. Getting URL Headers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 15.12. Setting URL Headers When Sending a Request
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. 15.13. Creating a GET Request Web Service with the Play Framework
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. 15.14. POSTing JSON Data to a Play Framework Web Service
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  19. 16. Databases and Persistence
    1. Introduction
    2. 16.1. Connecting to MySQL with JDBC
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 16.2. Connecting to a Database with the Spring Framework
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 16.3. Connecting to MongoDB and Inserting Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 16.4. Inserting Documents into MongoDB with insert, save, or +=
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 16.5. Searching a MongoDB Collection
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 16.6. Updating Documents in a MongoDB Collection
      1. Problem
      2. Solution
    8. 16.7. Accessing the MongoDB Document ID Field
      1. Problem
      2. Solution
      3. See Also
    9. 16.8. Deleting Documents in a MongoDB Collection
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 16.9. A Quick Look at Slick
  20. 17. Interacting with Java
    1. Introduction
    2. 17.1. Going to and from Java Collections
      1. Problem
      2. Solution
      3. Discussion
        1. Conversion tables
        2. Going from Scala collections to Java collections
        3. The JavaConverters object
      4. See Also
    3. 17.2. Add Exception Annotations to Scala Methods to Work with Java
      1. Problem
      2. Solution
      3. Discussion
    4. 17.3. Using @SerialVersionUID and Other Annotations
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 17.4. Using the Spring Framework
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 17.5. Annotating varargs Methods
      1. Problem
      2. Solution
      3. Discussion
    7. 17.6. When Java Code Requires JavaBeans
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 17.7. Wrapping Traits with Implementations
      1. Problem
      2. Solution
      3. Discussion
        1. Other attempts
  21. 18. The Simple Build Tool (SBT)
    1. Introduction
    2. 18.1. Creating a Project Directory Structure for SBT
      1. Problem
      2. Solution
        1. Use a shell script
        2. Use Giter8
        3. Giter8 notes
      3. Discussion
      4. See Also
    3. 18.2. Compiling, Running, and Packaging a Scala Project with SBT
      1. Problem
      2. Solution
      3. Discussion
        1. SBT commands
        2. Continuous compiling
        3. Use last to get more information on the last command
      4. See Also
    4. 18.3. Running Tests with SBT and ScalaTest
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 18.4. Managing Dependencies with SBT
      1. Problem
      2. Solution
      3. Discussion
        1. Repositories
      4. See Also
    6. 18.5. Controlling Which Version of a Managed Dependency Is Used
      1. Problem
      2. Solution
      3. See Also
    7. 18.6. Creating a Project with Subprojects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 18.7. Using SBT with Eclipse
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 18.8. Generating Project API Documentation
      1. Problem
      2. Solution
      3. See Also
    10. 18.9. Specifying a Main Class to Run
      1. Problem
      2. Solution
        1. Using run-main
      3. Discussion
      4. See Also
    11. 18.10. Using GitHub Projects as Project Dependencies
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 18.11. Telling SBT How to Find a Repository (Working with Resolvers)
      1. Problem
      2. Solution
      3. Discussion
    13. 18.12. Resolving Problems by Getting an SBT Stack Trace
      1. Problem
      2. Solution
    14. 18.13. Setting the SBT Log Level
      1. Problem
      2. Solution
      3. See Also
    15. 18.14. Deploying a Single, Executable JAR File
      1. Problem
      2. Solution
        1. Using sbt-assembly
      3. Discussion
      4. See Also
    16. 18.15. Publishing Your Library
      1. Problem
      2. Solution
    17. 18.16. Using Build.scala Instead of build.sbt
      1. Problem
      2. Solution
      3. Discussion
        1. The Full Configuration Example in the SBT documentation
      4. See Also
    18. 18.17. Using a Maven Repository Library with SBT
      1. Problem
      2. Solution
      3. See Also
    19. 18.18. Building a Scala Project with Ant
      1. Problem
      2. Solution
      3. Discussion
        1. The build.xml code
        2. Creating a JAR file with Ant
      4. See Also
  22. 19. Types
    1. Introduction
      1. Variance
      2. Bounds
      3. Type Constraints
      4. Type Examples in Other Chapters
    2. 19.1. Creating Classes That Use Generic Types
      1. Problem
      2. Solution
      3. Discussion
        1. Type parameter symbols
      4. See Also
    3. 19.2. Creating a Method That Takes a Simple Generic Type
      1. Problem
      2. Solution
      3. Discussion
    4. 19.3. Using Duck Typing (Structural Types)
      1. Problem
      2. Solution
      3. Discussion
    5. 19.4. Make Mutable Collections Invariant
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 19.5. Make Immutable Collections Covariant
      1. Problem
      2. Solution
      3. Discussion
    7. 19.6. Create a Collection Whose Elements Are All of Some Base Type
      1. Problem
      2. Solution
      3. Discussion
        1. Methods
      4. See Also
    8. 19.7. Selectively Adding New Behavior to a Closed Model
      1. Problem
      2. Solution
        1. Creating a type class
      3. Discussion
      4. See Also
    9. 19.8. Building Functionality with Types
      1. Example 1: Creating a Timer
      2. Example 2: Writing Your Own “Try” Classes
        1. The Scala 2.10 Try classes
  23. 20. Idioms
    1. Introduction
      1. Other Resources
    2. 20.1. Create Methods with No Side Effects (Pure Functions)
      1. Problem
      2. Solution
        1. Referential transparency
        2. Pure functions
        3. The Java approach
        4. Fixing the problems
      3. Discussion
        1. StockUtils or Stock object?
      4. See Also
    3. 20.2. Prefer Immutable Objects
      1. Problem
      2. Solution
      3. Discussion
        1. Using val + mutable, and var + immutable
        2. Summary
      4. See Also
    4. 20.3. Think “Expression-Oriented Programming”
      1. Problem
      2. Solution
      3. Discussion
        1. Benefits
      4. See Also
    5. 20.4. Use Match Expressions and Pattern Matching
      1. Problem
      2. Solution
        1. Replacement for the Java switch statement and unwieldy if/then statements
        2. In try/catch expressions
        3. As the body of a function or method
        4. Use with Option/Some/None
        5. In actors
        6. Summary
      3. See Also
    6. 20.5. Eliminate null Values from Your Code
      1. Problem
      2. Solution
        1. Initialize var fields with Option, not null
        2. Don’t return null from methods
        3. Converting a null into an Option, or something else
        4. Benefits
      3. See Also
    7. 20.6. Using the Option/Some/None Pattern
      1. Problem
      2. Solution
        1. Returning an Option from a method
        2. Getting the value from an Option
        3. Using Option with Scala collections
        4. Using Option with other frameworks
        5. Using Try, Success, and Failure
        6. Using Either, Left, and Right
      3. Discussion
        1. Don’t use the get method with Option
      4. See Also
  24. Index
  25. About the Author
  26. Colophon
  27. Copyright