The Scala Cookbook
Hello, my name is Alvin Alexander, and I wrote the Scala Cookbook for O'Reilly. Here's a link to The 2nd Edition of the Scala Cookbook:
The rest of this page contains direct links to recipes from the 1st Edition of the Scala Cookbook. Theese are all available on my main website, alvinalexander.com. And if you're new to the Scala programming language, you can find at scala-lang.org.
Preface
Chapter 1
Chapter 1 is not online yet.
Chapter 2
Chapter 2 is not online yet.
Chapter 3
- Recipe 3.1, How to loop over a collection with for and foreach (and how a for loop is translated)
- Recipe 3.2, How to use Scala for loops with multiple counters
- Recipe 3.3, How to use a 'for' loop with embedded 'if' statements (guards)
- Recipe 3.4, How to create a "for comprehension" (for/yield loop)
- Recipe 3.5, Scala: How to use break and continue in for loops (and while loops)
- Recipe 3.6, How to use a Scala if/then statement like a ternary operator
- Recipe 3.7, How to use a Scala match expression like a switch statement
- Recipe 3.8, How to match multiple conditions with one case statement
- Recipe 3.9, How to assign the result of a match expression to a variable
- Recipe 3.10, How to access the value of the default case in a Scala match expression
- Recipe 3.11, How to use pattern matching in Scala match expressions
- Recipe 3.12, How to use case classes in Scala match expressions
- Recipe 3.13, How to add 'if' expressions (guards) to case statements
- Recipe 3.14, How to use a match expression instead of isInstanceOf (to match types)
- Recipe 3.15, How to use Lists in Scala match expressions
- Recipe 3.16, How to match one or more exceptions with try/catch in Scala
- Recipe 3.17, How to declare a variable (var) before using it in try/catch/finally
- Recipe 3.18, How to create your own control structures in Scala
Chapter 4
- Recipe 4.1, How to create a primary constructor in Scala
- Recipe 4.2, How to control the visibility of Scala class constructor fields
- Recipe 4.3, How to define auxiliary class constructors
- Recipe 4.4, How to define a private primary constructor in Scala
- Recipe 4.5, How to provide default values for Scala constructor parameters
- Recipe 4.6, How to override default accessors and mutators in Scala classes
- Recipe 4.7, How to prevent getter and setter methods from being generated in Scala classes
- Recipe 4.8, How to assign a Scala class field to a block or function
- Recipe 4.9, How to set uninitialize var field types in Scala
- Recipe 4.10, How to handle constructor parameters when extending a Scala class
- Recipe 4.11, How to call a Scala superclass constructor
- Recipe 4.12, When to use an abstract class in Scala
- Recipe 4.13, How to define properties in an abstract base class or trait
- Recipe 4.14, How to generate boilerplate code with Scala case classes
- Recipe 4.15, How to define an equals method (object equality) in Scala
- Recipe 4.16, How to create inner classes in Scala
Chapter 5
- Recipe 5.1, How to control method scope in Scala (private, package, more)
- Recipe 5.2, How to call a method on a superclass in Scala
- Recipe 5.3, How to set default values for Scala method parameters
- Recipe 5.4, How to use parameter names when calling a Scala method
- Recipe 5.5, How to define Scala methods that returns multiple items (tuples)
- Recipe 5.6, How to force calling methods to leave parentheses off accessor (getter) methods
- Recipe 5.7, How to create methods that take variable-arguments (varargs) fields
- Recipe 5.8, How to declare that a Scala method can throw an exception
- Recipe 5.9, How to support a fluent style of programming in Scala
Chapter 6
Chapter 7
Chapter 8
- Recipe 8.1, How to use a Scala trait as an interface
- Recipe 8.2, How to use abstract and concrete fields in Scala traits
- Recipe 8.3, How to use a Scala trait like an abstract class
- Recipe 8.4, How to use Scala traits as simple mixins (or, How to mix in Scala traits)
- Recipe 8.5, How to limit which classes can use a trait by inheritance
- Recipe 8.6, How to mark a Scala trait so it can only be subclassed by a certain type
- Recipe 8.7, How to declare that a Scala trait can only be mixed into a type that has a specific method
- Recipe 8.8, How to dynamically add a Scala trait to an object instance
- Recipe 8.9, How to extend a Java interface like a Scala trait
Chapter 9
- Recipe 9.1, How to use function literals (anonymous functions) in Scala
- Recipe 9.2, How to use functions as variables (values) in Scala
- Recipe 9.3, How to define a method that accepts a simple function parameter
- Recipe 9.4, How to define Scala methods that take complex functions as parameters
- Recipe 9.5, How to use closures in Scala (closure examples, syntax)
- Recipe 9.6, How to use partially applied functions in Scala
- Recipe 9.7, How to create a method that returns a function in Scala
- Recipe 9.8, How to create and use partial functions in Scala
- Recipe 9.9, A real-world functional programming example in Scala
Chapter 10
- Introduction to Chapter 10 (Scala collection classes and methods)
- Recipe 10.1, Understanding the Scala collections hierarchy
- Recipe 10.2, How to choose a collection class in Scala
- Recipe 10.3, How to choose a Scala collection method to solve a problem
- Recipe 10.4, Understanding the performance of Scala collections
- Recipe 10.5, How to manually declare a type when creating a collection instance
- Recipe 10.6, Understanding Mutable Variables with Immutable Collections
- Recipe 10.7, Make the Vector Class Your 'Go To' Immutable Sequence
- Recipe 10.8, Make the Scala ArrayBuffer Class Your 'Go To' Mutable Indexed Sequence
- Recipe 10.9, How to loop over a Collection with foreach
- Recipe 10.10, How to Loop over a Collection with a for Loop
- Recipe 10.11, How to Use zipWithIndex or zip to Create Loop Counters
- Recipe 10.12, Using Iterators with Scala Collection Classes
- Recipe 10.13, How to Transform One Scala Collection to Another with for/yield
- Recipe 10.14, How to Transform One Scala Collection to Another With the map function (method)
- Recipe 10.15, How to flatten a List of Lists in Scala with flatten
- Recipe 10.16, How to Combine map and flatten with flatMap
- Recipe 10.17, How to use filter to Filter a Scala Collection
- Recipe 10.18, How to Extract a Sequence of Elements from a Scala Collection
- Recipe 10.19, How to split sequences into subsets in Scala (groupBy, partition, splitAt, span)
- Recipe 10.20, How to Walk Through a Scala Collection with the reduce and fold Methods
- Recipe 10.21, How to Extract Unique Elements from a Scala Sequence
- Recipe 10.22, How to Merge Scala Sequential Collections (List, Vector, ArrayBuffer, Array, Seq)
- Recipe 10.23, How to Merge Two Sequential Collections into Pairs with zip
- Recipe 10.24, How to Create a Lazy View on a Scala Collection
- Recipe 10.25, How to Populate a Scala Collection with a Range
- Recipe 10.26, How to Create and Use Scala Enumerations
- Recipe 10.27, Scala Tuples, for When You Just Need a Bag of Things
- Recipe 10.28, How to Sort a Scala Collection
- Recipe 10.29, How to Convert a Scala Collection to a String with mkString
Chapter 11
- Recipe 11.1, Different Ways to Create and Populate a List in Scala
- Recipe 11.2, How to Create a Mutable List in Scala (ListBuffer)
- Recipe 11.3, How to Add Elements to a List in Scala
- Recipe 11.4, How to Delete Elements from a List (or
ListBuffer
) in Scala
- Recipe 11.5, How to Merge (Concatenate) Lists in Scala
- Recipe 11.6, How to Use the Scala Stream Class, a Lazy Version of a List
- Recipe 11.7, Different ways to create and update an Array in Scala
- Recipe 11.8, How to Create an Array Whose Size Can Change (ArrayBuffer)
- Recipe 11.9, How to Delete Array and ArrayBuffer Elements in Scala
- Recipe 11.10, How to Sort Arrays in Scala
- Recipe 11.11, How to Create Multidimensional Arrays in Scala
- Recipe 11.12, How to Pass Arrays Back and Forth with Java Methods
- Recipe 11.13, How to Create Maps in Scala (Scala Map class examples)
- Recipe 11.14, How to Choose a Map Implementation in Scala
- Recipe 11.15, How to Add, Update, and Remove Elements with a Mutable Map
- Recipe 11.16, How to Add, Update, and Remove Elements with Immutable Maps
- Recipe 11.17, How to Access Map Values in Scala
- Recipe 11.18, How to Traverse a Map in Scala (for loop, foreach)
- Recipe 11.19, How to Get the Keys or Values from a Scala Map
- Recipe 11.20, How to Reverse Keys and Values in a Scala Map
- Recipe 11.21, How to Test for the Existence of a Key or Value in a Scala Map
- Recipe 11.22, How to Filter a Scala Map (filterKeys, retain, transform)
- Recipe 11.23, How to Sort an Existing Map by Key or Value (sortBy, sortWith)
- Recipe 11.24, How to Find the Largest Key or Value in a Scala Map
- Recipe 11.25, Going To and From Java Maps in Scala
- Recipe 11.26, How to Add Elements to a Set in Scala
- Recipe 11.27, How to Delete Elements from Sets in Scala
- Recipe 11.28, How to Use Sortable Sets in Scala
- Recipe 11.29, How to Use a Queue in Scala
- Recipe 11.30, How to Use a Stack in Scala
- Recipe 11.31, How to Use a Range in Scala
Chapter 12
- Recipe 12.1, How to open and read a text file in Scala
- Recipe 12.2, How to write text files in Scala
- Recipe 12.3, How to read and write binary files in Scala
- Recipe 12.4, How to process every character in a text file in Scala
- Recipe 12.5, How to process a CSV file in Scala
- Recipe 12.6, How to read a YAML configuration file in Scala
- Recipe 12.7, How to use a String like it's a file in Scala
- Recipe 12.8, How to use serialization in Scala (Serializable trait)
- Recipe 12.9, How to list files in a directory in Scala (and filtering them)
- Recipe 12.10, How to list subdirectories beneath a directory in Scala
- Recipe 12.11, How to execute external system commands in Scala
- Recipe 12.12, How to execute external commands and use their STDOUT in Scala
- Recipe 12.13, How to handle the STDOUT and STDERR of external commands
- Recipe 12.14, How to build a pipeline of external commands in Scala
- Recipe 12.15, How to redirect the STDOUT and STDIN of external commands
- Recipe 12.16, How to use 'and' and 'or' when running external commands in Scala
- Recipe 12.17, How to handle wildcard characters when running external commands
- Recipe 12.18, How to run an external command (process) in a different directory
- Recipe 12.19, How to set environment variables when running external commands
- Recipe 12.20, An index of methods available to run external system commands
Chapter 13
- Introduction to Chapter 13, Actors and Concurrency
- Recipe 13.1, How to get started with a simple Scala/Akka Actor
- Recipe 13.2, How to create a Scala/Akka Actor whose constructor requires arguments
- Recipe 13.3, How to communicate (send messages) between Scala/Akka actors
- Recipe 13.4, Understanding the methods in the Scala/Akka Actor lifecycle
- Recipe 13.5, How to start a Scala/Akka Actor
- Recipe 13.6, How to stop Akka Actors
- Recipe 13.7, How to shut down the Akka Actor system
- Recipe 13.8, Monitoring the death of an Akka Actor with 'watch'"
- Recipe 13.9, Simple concurrency with Scala Futures
- Recipe 13.10, How to send a message to an Akka Actor and wait for a reply
- Recipe 13.11, How to switch between different Akka Actor states with 'become'"
- Recipe 13.12, Examples of how to use parallel collections in Scala
Chapter 14
- Recipe 14.1, How to get started with the Scala REPL
- Recipe 14.2, How to paste and load blocks of code in the Scala REPL
- Recipe 14.3, How to add Jar files and classes to the REPL Classpath
- Recipe 14.4, How to run a shell command from the Scala REPL
- Recipe 14.5, How to compile Scala code with 'scalac' and run it with 'scala'
- Recipe 14.6, How to disassemble and decompile Scala code
- Recipe 14.7, How to find good Scala libraries
- Recipe 14.8, How to generate Scala documentation with the scaladoc command
- Recipe 14.9, How to compile your Scala code faster with the 'fsc' command-line compiler
- Recipe 14.10, How to use Scala as a scripting language
- Recipe 14.11, How to access command-line arguments in a Scala shell script
- Recipe 14.12, How to prompt users for input from Scala shell scripts
- Recipe 14.13, How to make your Scala shell scripts run faster by pre-compiling them
Chapter 15
- Recipe 15.1, How to create a JSON string from a Scala object
- Recipe 15.2, How to create a JSON String from Scala classes that have collections
- Recipe 15.3, How to create a simple Scala object from a JSON String
- Recipe 15.4, How to parse JSON data into an array of Scala objects
- Recipe 15.5, How to create simple web services with Scalatra
- Recipe 15.6, How to replace XML servlet mappings with Scalatra mounts
- Recipe 15.7, How to access Scalatra web service GET parameters
- Recipe 15.8, How to access POST request data with Scalatra
- Recipe 15.9, How to write a simple HTTP GET request client in Scala
- Recipe 15.10, How to create a Twitter client in Scala
- Recipe 15.11, How to send JSON POST data to a REST url
- Recipe 15.12, How to access HTTP response headers after making an HTTP request with Apache HttpClient
- Recipe 15.13, How to set HTTP headers when sending a web service request
- Recipe 15.14, How to write a GET request web service with the Play Framework
- Recipe 15.15, How to write a Play Framework POST request web service
Chapter 16
- Recipe 16.1, How to connect to a MySQL database with Scala and JDBC
- Recipe 16.2, How to connect to a database with the Spring Framework
- Recipe 16.3, How to connect to a MongoDB and insert data with Scala
- Recipe 16.4, How to insert documents into MongoDB with insert, save, and +=
- Recipe 16.5, How to search a MongoDB collection with Scala and Casbah
- Recipe 16.6, How to update documents in a MongoDB collection with Casbah
- Recipe 16.7, How to access the MongoDB document 'ID' field (_id) with Casbah
- Recipe 16.8, How to delete documents in a MongoDB with Casbah
- Recipe 16.9, A quick look at the Slick database library
Chapter 17
Chapter 18
Chapter 19
Chapter 20