This is a cookbook of problem-solving recipes about Scala, the most interesting programming language Ive ever used. The book contains solutions to more than 250 common problems, shown with possibly more than 700 examples. (I havent counted, but I suspect thats true.)
Just prior to its release, the book was updated to cover Scala 2.10.x and SBT 0.12.3.
1. The Scala Language
My (oversimplified) Scala elevator pitch is that its a child of Ruby and Java: its 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 its 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, its 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, its a power language created for the professional programmer. Dont 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 Id say, Use Scala.
Here are a few more nuggets about Scala:
Its a modern programming language created by Martin Odersky (the father of javac
), influenced by Java, Ruby, Smalltalk, ML, Haskell, Erlang, and others.
Its a pure object-oriented programming (OOP) language. Every variable is an object, and every operator is a method.
Its 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 youll keep learning and finding newer, better ways to write code. Scala will change the way you think about programmingand thats a good thing.
Of all of Scalas 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 thats concise and readable is a big deal. Because Scala has these attributes, programmers say that its expressive .
Solutions
Ive always bought OReilly cookbooks for the solutions, and thats 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
, whats 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 its nice when a recipe says, Hey, I know youre here to read about how to loop over the elements in an Array
, heres how you do that:
for
(
i
<-
Array
(
1
,
2
,
3
))
println
(
i
)
But, if what youre really trying to do is transform those elements into a new collection, what you want is a for/yield expression or map
method:
// for/yieldscala>
for (i <- Array(1,2,3)) yield i * 2
res0: Array[Int] = Array(2, 4, 6)// mapscala>
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; whats the proper way to organize an application?
It looks like SBT is the best build tool for Scala, but its 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 shouldnt 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 whats 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?