The Artima Developer Community


Errata for Programming in Scala, 2nd Edition
Chapter 3: Next Steps in Scala
Return to errata index.

Page 38 (PDF page 80):
Discussion of arrays should describe how to create/use multidimensional
arrays in Scala.
Page 42 (PDF page 86):
location 968 of 16616

Chapter 3 Step8

Scala's LIst, scala.List, differs from Java's java.util.List...

My feedback: No scala.List there but scala.collection.immutable.List

right?
Page 43 (PDF page 87):
Description of Why not append to lists is inaccurate.  Please see the
following.
Your options if you want to build a list efficiently by appending
elements is to prepend them, then when you're done call reverse.

Example - Append 4 to List(1,2,3) is expected to be List(1,2,3,4).
In Scala interpretor, you get List(3,2,1,4) which is not expected.
scala> val l1 = List(1,2,3)
l1: List[Int] = List(1, 2, 3)

scala> (4 :: l1).reverse
res17: List[Int] = List(3, 2, 1, 4)
Page 45 (PDF page 89):
Deprecation warnings for some examples:

scala> val thrill = "Will" :: "fill" :: "until" :: Nil
thrill: List[java.lang.String] = List(Will, fill, until)

scala> thrill.remove(s => s.length == 4)
warning: there were deprecation warnings; re-run with -deprecation for
details
res13: List[java.lang.String] = List(until)

scala> thrill.sort((s, t) => s.charAt(0).toLower < t.charAt(0).toLower)
warning: there were deprecation warnings; re-run with -deprecation for
details
res14: List[java.lang.String] = List(fill, until, Will)
Page 45 (PDF page 89):
With Scala 2.11.7, this example:

thrill.sort((s, t) => s.charAt(0).toLower < t.charAt(0).toLower)

now not only is deprecated (as stated by an existing note), but fails:

scala> thrill.sort((s, t) => s.charAt(0).toLower < t.charAt(0).toLower)
<console>:12: error: value sort is not a member of List[String]
       thrill.sort((s, t) => s.charAt(0).toLower < t.charAt(0).toLower)


Instead, use sortWith:

scala> thrill.sortWith((s, t) => s.charAt(0).toLower <
t.charAt(0).toLower)
res39: List[String] = List(fill, until, Will)
Page 46 (PDF page 90):
"... but unlike lists, tuples can contain different types of elements".

List(4, "5", 6.0)

compiles well, containing an Int, String and Double.
Page 46 (PDF page 90):
It is written:
Scala infers the type of the tuple to be Tuple2[Int, String]

However, in Scala 2.11.6 REPL, the code
scala> val pair = (99, "luft") //gives
pair: (Int, String) = (99,luft)

Thus, no mention of type as given in book to be
Tuple2[Int, String]
Page 48 (PDF page 92):
"As this example shows, you can create sets in Scala similarly to how you
create lists and arrays"

Should it be 'similar' instead of 'similarly' ?
Page 48 (PDF page 92):
In the second paragraph: 

"Both mutable and immutable sets offer a + method, but their behavior
differs."

This is not true, the + methods of both kinds of sets do exactly the same
thing.
Page 48 (PDF page 92):
"Both mutable and immutable sets offer a + method, but their behavior
differs. Whereas a mutable set will add the element to itself, an
immutable
set will create and return a new set with the element added."

This statement is incorrect, + semantics does not change. Example below:

import scala.collection.mutable.Set

val first = Set("A", "B")
val second = first + "C"
println(first)
println(second)

"first" set is not altered.
Page 83 (PDF page 97):
location 1238 of16616 : Amazon eBook
def formatArgs(arg: Array[String]) = args.mkString("\n")
...
Thus if args contains three elements "zero", "one" and "two", formatArgs
will return "zero\one\two".

My feedback:
#1 
 from: def formatArgs(arg: Array[String]) = args.mkString("\n")
 to:   def formatArgs(arg: Array[String]) = args.mkString("\\")
or
#2
 from: formatArgs will return "zero\one\two".
 to:   formatArgs will return 
       zero
       one
       two

EndOfMyFeedback
Page 56 (PDF page 100):
Source.fromPath should be Source.fromFile

[FIXED]
Page 56 (PDF page 100):
"The getLines method returns an Iterator[String], which provides one line
on each iteration, including the end-of-line character."

The end-of-line character is not included, according to the API
documentation of Source.getLines().

[FIXED]
Page 56 (PDF page 100):
$ scala -version
Scala code runner version 2.8.0.final -- Copyright 2002-2010, LAMP/EPFL
$ cat countchars1.scala 
import scala.io.Source 

if (args.length > 0) {

  for (line <- Source.fromPath(args(0)).getLines()) 
    println(line.length +" "+ line)
} 
else
  Console.err.println("Please enter filename")
$ scala countchars1.scala countchars1.scala 
/Users/binil/temp/countchars1.scala:5: error: value fromPath is not a
member of object scala.io.Source
  for (line <- Source.fromPath(args(0)).getLines()) 
                      ^
one error found
$

[FIXED]

Page number: Book type: Paperback book PDF eBook
Book version: (Or build date. Found on back of title page.)
Your feedback:
Your name: (optional)
Your email address: (optional) (will not be published)

Copyright © 2024 Artima, Inc. All rights reserved.