Errata

Scala Cookbook

Errata for Scala Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Printed Page all source code
all source code

The source code for the first edition does not exist at the book's indicated site, or anywhere else I can find.
The source code for the second edition is only partially available.



William Thomas  Jun 05, 2023 
3.15
The 'Sum' method before the Discussion section towards the end

The return value for case Nil should be 0, so that the sum method correctly evaluates to 15, rather than incorrectly to 16 as it does in the ebook

def sum(list: List[Int]): Int = list match {
case Nil => 1
case n :: rest => n + sum(rest)
}

scala> val nums = List(1,2,3,4,5)
nums: List[Int] = List(1, 2, 3, 4, 5)

scala> sum(nums)
res0: Int = 16

Naman Jain  Jul 20, 2017 
Printed Page 42
2nd line

The method

def ~=(x: Double, y: Double, precision: Double) = {
if ((x - y).abs < precision) true else false
}

should IMO be shortened to just

def ~=(x: Double, y: Double, precision: Double): Boolean = (x - y).abs < precision

and could be generalized to all Numeric types:

def ~=[T](x: T, y: T, precision: T)(implicit num: Numeric[T]): Boolean = {
import num.{mkNumericOps, mkOrderingOps} // for infix operators
(x - y).abs < precision
}

Michael Hübner  Mar 05, 2017 
Printed Page 44
3rd line in section 'Discussion'

"BigInt and BigDecimal ... and they're also mutable (as you saw in the += example)."

is not correct as BigInt and BigDecimal objects are immutable.
What the author wants to express is that the assignment operators like += are applicable (which create a new value object and assign this to the variable).

Michael Hübner  Mar 05, 2017 
Printed Page 77
Solution for 3.9

The solution for problem 3.9:

val evenOrOdd = someNumber match {
case 1 | 3 | 5 | 7 | 9 => println("odd")
case 2 | 4 | 6 | 8 10 => println("even")
}

It should be:

val evenOrOdd = someNumber match {
case 1 | 3 | 5 | 7 | 9 => "odd"
case 2 | 4 | 6 | 8 10 => "even"
}

The println command don't return any value.

Rolf Speer  Jun 24, 2014 
Printed Page 83
section "Constructor patterns"

The sentence
"The constructor pattern lets you match a constructor in a case statement."
should be changed to:
"The constructor pattern lets you match a constructor of a case class."

This statement is more precise (because the pattern only works for case classes) and is more general (because constructor patterns can also be used in decomposing assignments, not only in case statements).

Michael Hübner  Mar 11, 2017 
PDF Page 90
3rd paragraph

def sum(list: List[Int]): Int = list match {
case Nil => 1
case n :: rest => n + sum(rest)
}


should be:


def sum(list: List[Int]): Int = list match {
case Nil => 0
case n :: rest => n + sum(rest)
}


scala> sum(nums)
res0: Int = 16

should be:

scala> sum(nums)
res0: Int = 15

Hichem Essafi  Nov 10, 2013 
ePub Page 95
Last paragraph

?As you saw in Recipe 1.11?

Recipe doesn't exist - supposed to be 1.10?

Anonymous  Oct 08, 2013 
Printed Page 131
line 11

"The following example demonstrates an Animal trait ..."

should be

"The following example demonstrates a Pet class ..."

Alexander Mentis  Feb 11, 2015 
PDF Page 155
Solution for 5.3

There is:
class Connection {
def makeConnection(timeout: Int = 5000, protocol: = "http") {
println("timeout = %d, protocol = %s".format(timeout, protocol))
// more code here
}
}

It should be:
class Connection {
def makeConnection(timeout: Int = 5000, protocol: String = "http") {
println("timeout = %d, protocol = %s".format(timeout, protocol))
// more code here
}
}

protocol argument needs type, check in Scala version 2.10.1

Anonymous  Aug 27, 2013 
PDF Page 195
last paragraph

In the first paragraph of Discussion in recipe 7.3:

"The book Programming in Scala, by Odersky, et al (Artima). The book refers to this as a renaming clause."

should be

"The book Programming in Scala, by Odersky, et al (Artima) refers to this as a renaming clause."

Anonymous  Jun 14, 2014 
PDF Page 203
3rd paragraph

"Traits have much more power than Java interfaces because, just like abstract methods in Java, they can also have implemented methods. However, unlike Java?s abstract classes, you can mix more than one trait into a class, and a trait can also control what classes it can be mixed into."

"abstract methods" in the first sentence should be "abstract classes".

Anonymous  Jun 14, 2014 
PDF Page 203
3rd paragraph

"Traits have much more power than Java interfaces because, just like abstract methods in Java, they can also have implemented methods."

I believe this should be:

"Traits have much more power than Java interfaces because, just like abstract classes in Java, they can also have implemented methods."

Please let me know if I am wrong :)

Erik Innocent  Aug 06, 2015 
PDF Page 206
Par 8.2 - Section: Discussion

Problem: code example states that 'override' is required while it isn't

Following code sample illustrates when override is required and when not:

// Case 1: uninitialized val field
trait PizzaTrait {
val maxNumToppings: Int
}

class Pizza extends PizzaTrait {
val maxNumToppings = 10 // 'override' is not required
}

// Case 2: initialized val field
trait PizzaTrait {
val maxNumToppings = 10
}

class Pizza extends PizzaTrait {
override val maxNumToppings = 11 // 'override' is required
}

Scala version: 2.10.1

Eric Loots  Aug 20, 2013 
Printed Page 222
1st paragraph

There are two bit.ly links, neither of which work:

http://bit.ly/13KD90C
http://bit.ly/13KD9OC

Note that the first one uses a 'zero' and the second uses a capital 'o'.

Eric Smith  Nov 02, 2013 
PDF Page 337
See Also

The See Also for section 11.10 Sorting Arrays references Recipe 10.29. It should reference Recipe 10.28.

Allan Jacobs  Jan 11, 2015 
Printed, PDF Page 383
Example code

The code as written cannot work, as Source.getLines does not contain newline characters:
Returns an iterator who returns lines (NOT including newline character(s))

Instead we could write this as:

def countlines3(source: io.Source) : Long = {
var lineCount = 0L

for { l <- source.getLines; if source.hasNext}
lineCount += 1

lineCount
}

Or even more idiomatic:

val lineCount = scala.io.Source.fromFile("examplefile.txt").getLines.size

Philipp Weissenbacher  Mar 22, 2016 
PDF Page 395
At the end of the directory list resulting from "ls -al".! the variable printed should be "exitCode" not "result.".

The result of doing:

"ls -al".!

should be the listing but the text after the listing should be:

exitCode: Int = 0

not:

result: Int = 0 as the text has it.

Kurt Berg  Feb 11, 2014 
PDF Page 463
code example

The first comment in the body of the object ("used by the time method") and the following line of code don't seem to relate to this example.

Also, isn't the sleep call at the end of the method redundant given that we are blocking on the Future in the preceding lines ?

Michael Bedward  Nov 27, 2013 
PDF Page 615
Last paragraph

Last paragraph of the page reads:

"You can pass your current dogs instance into this method, but you won’t be able to pass the following superDogs collection into makeDogsSpeak:"

makeDogsSpeak should be printDogTypes

Subsequently, the next page, page 616, also have makeDogsSpeak instead of printDogTypes

Kevin Gomulia  Mar 09, 2020 
PDF Page 647
1st line of the page

"I didn’t use this approach because I didn’t want to expose toppings as an immutable collection outside of my Pizza class, ... "

It should be "mutable" instead of "immutable".

val toppings = new collection.mutable.ArrayBuffer[Topping]()

ArrayBuffer is mutable.

Charlie Zhu  Apr 02, 2018