The Artima Developer Community


Errata for Programming in Scala
Chapter 7: Built-in Control Structures
Return to errata index.

Page 117 (PDF page 151):
gcdLoop should work like gcd in
Listings 6.3, 6.5, 7.4
-- `exchange' parameters x and y.

def gcdLoop(x: Long, y: Long): Long = {
  var a = x
  var b = y
  while (b != 0) {
    val temp = b
    b = a % b
    a = temp
  }
  a
}
Page 124 (PDF page 158):
Listing 7.9
Add a semicolon after "line <- fileLines(file)".
Page 125 (PDF page 159):
Listing 7.10
Add a semicolon after "line <- fileLines(file)".
Page 125 (PDF page 159):
Second paragraph:
Replace "For each of these it generates an Iterator[String]..." by "For
each of these it generates an List[String]...".
Remove the following sentence ("An Iterator offers...").
Replace "This initial iterator is transformed into another
Iterator[String]..." by "This initial list is transformed into another
List[String]...".
Page 132 (PDF page 166):
Listing 7.16 ยท Looping without break or continue.
Will yield the wrong index. The statement
i = i + 1
is executed even if the string is found. It should be
if (!foundIt) i = i + 1
Page 132 (PDF page 166):
The while loop in Listing 7.16 should not increase the i var when foundIt
== true. Otherwise, the value of i would always be 1 more than in the
example in Listing 7.15.

Great book btw, thanks.
Page 124 (PDF page 168):
Some users reported an error in the listing 7.9 (pag. 124) that in my
opinion it is not an error.

The user says to add a semicolon at the end of
"line <- fileLines(file)"

Why that? We are using curly braces, not parenthesis, so the semicolon is
not needed! 
If not, I think we have to review the rules of the semicolon inference
(see chapter 4.2)

To be honest I did not test that code, but from what it is written in the
book, I think the example it is correct.

Please let me know
Page 124 (PDF page 168):
It is written "To do so, you prefix the body of the for expression by the
keyword yield"
I think it is wrong the word 'prefix', because the keyword 'yield' is the
at the end of the body, not before (postfix)
Page 125 (PDF page 169):
Page 125 Listing 7.10
The code is unsafe. A better solution is to add another if clause
"file.isFile" to check that there is a non-file type in the filesHere
list before getting the lines for the file in the fileList.  

I found this bug when I was testing printScalaFiles2() and
printScalaFiles3() to see if they were functionally different.  My test
was to add a directory named test.dir.scala.  Then I ran the sample code
in Files.scala that is on the web for this book.  I get a the error:

java.io.FileNotFoundException: ./test.dir.scala (Is a directory)

I spent time trying to figure this out, fixing/documenting it would be
cool so others do not run into the same problem.

The safe/correct code is:

  val forLineLengths =
    for {
      file <- filesHere
      if file.isFile
      if file.getName.endsWith(".scala")
      line <- fileLines(file)
      trimmed = line.trim
      if trimmed.matches(".*for.*")  
    } yield trimmed.length
Page 125 (PDF page 169):
Mmm, I just filed an errata request to add an if clause for "file.isFile"
on the listing 7.10 "val forLineLengths".  But, it just dawned on me that
the same "if file.isFile" clause is needed on listing 7.8 and and listing
7.9.  Seems like it should be standard operating procedure for this type
of algorithm.  Which also implies there should be a file-only access
method in java.io.File...  Perhaps
have.io.File(<dirName>).listFiles.isFiles access method...

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 © 2021 Artima, Inc. All rights reserved.