1. The Scala Language

My (oversimplified) Scala elevator pitch is that it’s a child of Ruby and Java: it’s light, concise, and readable like Ruby, but it compiles to class files that you package as JAR files that run on the JVM; it uses traits and mixins, and feels dynamic, but it’s statically typed. It uses the Actor model to simplify concurrent programming so you can keep those multicore processors humming. The name Scala comes from the word scalable, and true to that name, it’s used to power the busiest websites in the world, including Twitter, Netflix, Tumblr, LinkedIn, Foursquare, and many more.

In my opinion, Scala is not a good language for teaching a Programming 101 class. Instead, it’s a power language created for the professional programmer. Don’t let that scare you, though. If you were my own brother and about to start a new project and could choose any programming language available, without hesitation I’d say, “Use Scala.”

Here are a few more nuggets about Scala:

  • It’s a modern programming language created by Martin Odersky (the father of javac), influenced by Java, Ruby, Smalltalk, ML, Haskell, Erlang, and others.

  • It’s a pure object-oriented programming (OOP) language. Every variable is an object, and every “operator” is a method.

  • It’s also a functional programming (FP) language, so you can pass functions around as variables. You can write your code using OOP, FP, or both.

  • Scala code runs on the JVM and lets you use the wealth of Java libraries that have been developed over the years.

  • You can be productive on Day 1, but the language is deep, so as you go along you’ll keep learning and finding newer, better ways to write code. Scala will change the way you think about programming—and that’s a good thing.

Of all of Scala’s benefits, what I like best is that it lets you write concise, readable code. The time a programmer spends reading code compared to the time spent writing code is said to be at least a 10:1 ratio, so writing code that’s concise and readable is a big deal. Because Scala has these attributes, programmers say that it’s expressive.

Solutions

I’ve always bought O’Reilly cookbooks for the solutions, and that’s what this book is about: solving problems.

When using a cookbook, I usually think, “I have this problem, I need to iterate over the elements in an Array, what’s the best way to do that?” I like to look at the table of contents, find a recipe, implement the solution, and move on. I tried to write each recipe with this use case in mind.

However, with a modern language like Scala, it may end up that I phrased my question wrong. Because of my prior programming experience I may have thought, “I need to iterate over the elements in an Array,” but in reality my deeper intent was to loop over those elements for a reason, such as to transform them into a new collection. So it’s nice when a recipe says, “Hey, I know you’re here to read about how to loop over the elements in an Array, here’s how you do that”:

for (i <- Array(1,2,3)) println(i)

“But, if what you’re really trying to do is transform those elements into a new collection, what you want is a for/yield expression or map method”:

// for/yield
scala> for (i <- Array(1,2,3)) yield i * 2
res0: Array[Int] = Array(2, 4, 6)

// map
scala> Array(1,2,3).map(_ * 2)
res1: Array[Int] = Array(2, 4, 6)

(More on that _ character shortly.)

To create the list of problems and solutions, I followed the “Eat your own dog food” philosophy. The recipes come from my own experience of creating Scala scripts, web applications, web services, Swing applications, and actor-based systems. As I developed the applications I needed, I encountered problems like these:

  • Scala files tend to be very small; what’s the proper way to organize an application?

  • It looks like SBT is the best build tool for Scala, but it’s different than Ant or Maven; how do I compile and package applications, and work with dependencies?

  • Constructors are really different than Java; how do I create them? What code is generated when I declare constructor parameters and class fields?

  • Actors are cool; how do I write a complete actor-based application?

  • What, I shouldn’t use null values anymore? Why not? How do I code without them?

  • I can pass a function around like any other variable? How do I do that, and what’s the benefit?

  • Why are there so many collections classes, and why does each collection class have so many methods?

  • I have all of this legacy Java code; can I still use it in Scala? If so, how?

  • I’m starting to grok this. Now I need to know, what are the top five or ten “best practices” of writing Scala code?

Truthfully, I fell fast in love with everything about Scala except for one thing: the collections library seemed large and intimidating. I really enjoyed using Scala so I kept using the language, but whenever I needed a collection, I used a trusty old Java collection.

Then one day I got up the courage to dive into the collections library. I thought I’d hate it, but after struggling with it for a while, I suddenly “got” it. The light bulb went on over my head, and I suddenly understood not only the collections, but several other concepts I had been struggling with as well. I realized the collections library writers aren’t crazy; they’re brilliant.

Once I understood the collections library, I quit writing so many for loops, and started using collection methods like filter, foreach, and map. They made coding easier, and made my code more concise. These days I can’t imagine a better way to write code like this:

// filter the items in a list
scala> val nums = List(1,2,3,4,5).filter(_ < 4)
nums: List[Int] = List(1, 2, 3)

The _ wildcard character is discussed in several recipes, but as you can infer from that example, it’s a placeholder for each element in the collection. The filter method loops through each element in the list, calling your _ < 4 function on each iteration. That Scala one-liner is the equivalent of this Java code:

Integer[] intArray = {1,2,3,4,5};
List<Integer> nums = Arrays.asList(intArray);
List<Integer> filteredNums = new LinkedList<Integer>();
for (int n: nums) {
  if (n < 4) filteredNums.add(n);
}

The next example takes this a step further. It filters the elements as in the previous example, and then multiplies each element by the number 2 using the map method:

// filter the items, then double them
scala> val nums = List(1,2,3,4,5).filter(_ < 4).map(_ * 2)
nums: List[Int] = List(2, 4, 6)

If you think about how much code would be required to write this expression in another language, I think you’ll agree that Scala is expressive.

(If you’re new to Scala, examples like this are broken down into smaller chunks in the recipes.)

Audience

This book is intended for programmers who want to be able to quickly find solutions to problems they’ll encounter when using Scala and its libraries and tools. I hope it will also be a good tool for developers who want to learn Scala. I’m a big believer in “learning by example,” and this book is chock full of examples.

I generally assume that you have some experience with another programming language like C, C++, Java, Ruby, C#, PHP, Python, or similar. My own experience is with those languages, so I’m sure my writing is influenced by that background.

Another way to describe the audience for this book involves looking at different levels of software developers. In the article at scala-lang.org, Martin Odersky defines the following levels of computer programmers:

  • Level A1: Beginning application programmer

  • Level A2: Intermediate application programmer

  • Level A3: Expert application programmer

  • Level L1: Junior library designer

  • Level L2: Senior library designer

  • Level L3: Expert library designer

This book is primarily aimed at the application developers in the A1, A2, A3, and L1 categories. While helping those developers is my primary goal, I hope that L2 and L3 developers can also benefit from the many examples in this book—especially if they have no prior experience with functional programming, or they want to quickly get up to speed with Scala and its tools and libraries.

Contents of This Book

The first three chapters in this book cover some of the nuts and bolts of the Scala language.

Chapter 1, Strings, provides recipes for working with strings. Scala gets its basic String functionality from Java, but with the power of implicit conversions, Scala adds new functionality to strings through classes like StringLike and StringOps, which let Scala treat a String as a sequence of Char. The last recipe in the chapter shows how to add your own behavior to a String (or any other class) by creating an implicit conversion.

Chapter 2, Numbers, provides recipes for working with Scala’s numeric types. There are no ++ and −− operators for working with numbers, and this chapter explains why, and demonstrates the other methods you can use. It also shows how to handle large numbers, currency, and how to compare floating-point numbers.

Chapter 3, Control Structures, demonstrates Scala’s built-in control structures, starting with if/then statements and for loops, and then provides solutions for working with for/yield loops (for comprehensions) and for expressions with embedded if statements (guards). Because match expressions are so important to Scala, several recipes show how to use them to solve a variety of problems.

The next five chapters continue to cover the Scala syntax, with an emphasis on organizing your projects with classes, methods, objects, traits, and packaging. Recipes on classes, methods, objects, and traits place an emphasis on object-oriented programming techniques.

Chapter 4, Classes and Properties, provides examples related to Scala classes and fields. Because Scala constructors are very different than Java constructors, several recipes show the ins and outs of writing both primary and auxiliary constructors. The chapter also shows how to override the accessor and mutator methods that Scala automatically generates for your val and var variables. Several recipes show what case classes are and how to use them, and how to write equals methods.

Chapter 5, Methods, shows how to define methods to accept parameters, return values, use parameter names when calling methods, set default values for method parameters, create varargs fields, and write methods to support a fluent style of programming.

Chapter 6, Objects, covers “all things object.” Like Java, Scala uses the word object to refer to an instance of a class, but Scala also has an object keyword. This chapter covers topics like class casting, how to launch an application with an object, how to create the equivalent of Java’s static members, and how to write a class with a companion object so you can create new instances of a class without using the new keyword.

Chapter 7, Packaging and Imports, provides examples of Scala’s package and import statements, which provide more capabilities than the same Java keywords. This includes how to use the curly brace style for packaging, how to hide and rename members when you import them, and more.

Chapter 8, Traits, provides examples of the Scala trait. It begins by showing how to use a trait like a Java interface, and then gets into more advanced topics, such as how to use traits as “mixins,” and limit which members a trait can be mixed into using a variety of methods.

Although much of the book demonstrates functional programming (FP) techniques, Chapter 9, Functional Programming, combines many FP recipes into one location. Solutions show how to define anonymous functions (function literals) and use them in a variety of situations. Recipes demonstrate how to define a method that accepts a function argument, how to return a function from a function, and how to use closures and partially applied functions.

The Scala collections library is rich and deep, so Chapter 10, Collections, and Chapter 11, List, Array, Map, Set (and More), provide more than 100 pages of collection-related solutions.

Recipes in Chapter 10, Collections, help you choose collection classes for specific needs, and then help you choose and use methods within a collection to solve specific problems, such as transforming one collection into a new collection, filtering a collection, and creating subgroups of a collection. More than 60 pages of recipes demonstrate solutions for writing for loops, for/yield expressions, using methods like filter, foreach, groupBy, map, and many more.

Chapter 11, List, Array, Map, Set (and More), continues where Chapter 10, Collections, leaves off, providing solutions for those specific collection types, as well as recipes for the Queue, Stack, and Range classes.

Chapter 12, Files and Processes, begins by providing solutions about reading and writing files with Scala, including CSV. After that, because the Scala library makes it much (much!) easier to work with external processes than Java, a collection of recipes demonstrates how to execute external commands and work with their I/O.

Chapter 13, Actors and Concurrency, provides solutions for the wonderful world of building concurrent applications (and engaging those multicore CPUs) with the Scala Actors library. Recipes in this chapter show solutions to common problems using the industrial-strength Akka Actors library that was integrated into the 2.10.x Scala release. Examples show how to build actor-based applications from the ground up, how to send messages to actors, how to receive and work with messages in actors, and how to kill actors and shut down the system. It also shows easy ways to run concurrent tasks with a Future, a terrific way to run simple computations in parallel.

Chapter 14, Command-Line Tasks, combines a collection of recipes centered around using Scala at the command line. It begins by showing tips on how to use the Scala REPL, and then shows how to use command-line tools like scalac, scala, scaladoc, and fsc. It also provides recipes showing how to use Scala as a scripting language, including how to precompile your Scala scripts to make them run faster.

Chapter 15, Web Services, shows how to use Scala on both the client and server sides of web services. On the server side, it shows how to use Scalatra and the Play Framework to develop RESTful web services, including how to use Scalatra with MongoDB. For both client and server code, it shows how to serialize and deserialize JSON and how to work with HTTP headers.

Chapter 16, Databases and Persistence, provides examples of how to interact with databases from Scala, including working with traditional SQL databases using JDBC and Spring JDBC, along with extensive coverage of how to work with MongoDB, a popular “NoSQL” database.

Chapter 17, Interacting with Java, shows how to solve the few problems you’ll encounter when working with Java code. While Scala code often just works when interacting with Java, there are a few gotchas. This chapter shows how to resolve problems related to the differences in the collections libraries, as well as problems you can run into when calling Scala code from Java.

Chapter 18, The Simple Build Tool (SBT), is a comprehensive guide to the de-facto build tool for Scala applications. It starts by showing several ways to create an SBT project directory structure, and then shows how to include managed and unmanaged dependencies, build your projects, generate Scaladoc for your projects, deploy your projects, and more. Though I strongly recommend learning SBT, a recipe also shows how to use Ant to compile Scala projects.

Chapter 19, Types, provides recipes for working with Scala’s powerful type system. Starting right from the introduction, concepts such as type variance, bounds, and constraints are demonstrated by example. Recipes demonstrate how to declare generics in class and method definitions, implement “duck typing,” and how to control which types your traits can be mixed into.

Chapter 20, Idioms, is unique for a cookbook, but because this is a book of solutions, I think it’s important to have a section dedicated to showing the best practices, i.e., how to write code “the Scala way.” Recipes show how to create methods with no side effects, how to work with immutable objects and collection types, how to think in terms of expressions (rather than statements), how to use pattern matching, and how to eliminate null values in your code.

Online Bonus Chapters

Because Scala is an incredibly rich and deep language, an additional three chapters consisting of more than 130 pages of Scala Cookbook content are available for readers who wish to explore Scala further. These bonus chapters are:

  • XML and XPath

  • Testing and Debugging

  • The Play Framework

These chapters are available in PDF format, and can be downloaded at http://examples.oreilly.com/9781449339616-files/.

Installing the Software

Installing Scala is simple and should just take a few minutes.

On Unix systems (including Mac OS X), download the software from the Scala download page to a directory on your computer like $HOME/scala, and then add these lines to your $HOME/.bash_profile file (or its equivalent, depending on which login shell you’re using):

export SCALA_HOME=/Users/Al/scala
PATH=$PATH:/Users/Al/scala/bin

Once you’ve done this, when you open a new terminal window, you should have access to the scala and scalac commands at your command line.

You can follow a similar process if you’re using Microsoft Windows, or you can use an MSI installer. See the Scala download page for more information.

How the Code Listings Work

Most of the code listings in the book are shown in the Scala “Read-Eval-Print-Loop,” or REPL. If you’ve used irb with Ruby, the concept is the same: you type an expression, and the REPL evaluates the expression and prints the resulting output.

In the REPL examples, the code that’s shown in a bold font is what you type, and all the text that isn’t bold is output from the REPL.

You start the REPL from your operating system command line by executing the scala command:

$ scala
Welcome to Scala version 2.10.1
Type in expressions to have them evaluated.
Type :help for more information.

scala> _

Once the REPL has started, just type your expressions as input, and the REPL will evaluate them and show their output:

scala> val hello = "Hello, world"
hello: String = Hello, world

scala> Array(1,2,3).foreach(println)
1
2
3

The REPL is demonstrated more in the Chapter 1 introduction and Recipe 14.1. Recipe 14.4 takes this a step further and shows how to customize the REPL environment.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This icon signifies a tip, suggestion, or general note.

Caution

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/alvinj.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Scala Cookbook by Alvin Alexander (O’Reilly). Copyright 2013 Alvin Alexander, 978-1-449-33961-6.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/Scala_CB.

To comment or ask technical questions about this book, send an email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Writing a book this large takes a lot of work, and I’d like to thank my editor, Courtney Nash, for keeping me sane during the speed bumps and generally being encouraging throughout the process.

Kim Cofer was the copy editor for this book, and I’d like to thank her for helping whip the book into shape, correcting my grammar issues regardless of how many times I repeated them, and for having good discussions about how to handle several issues in this book.

This book grew from about 540 pages during the first review to roughly 700 pages in its final release, and much of that was due to reviewers. All of the reviewers were helpful in different ways, but I’d especially like to thank Eric Torreborre and Ryan LeCompte for making it all the way through different versions of the book. Additional thanks go out to Rudi Farkas, Rahul Phulore, Jason Swartz, Hugo Sereno Ferreira, and Dean Wampler.

I’d also like to thank my friends and family members who encouraged me throughout the process. A special thanks goes to my sister Melissa, who helped bring my initial plain, wiki-style text into Microsoft Word, and styled everything correctly.

Finally, I’d like to thank Martin Odersky and his team for creating such an interesting programming language. I also owe his Programming Methods Laboratory at EFPL a special thank you for letting me use the Scala collections performance tables shown in Recipe 10.4.

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.