Functional Programming in Scala Errata

Thank you for purchasing Functional Programming in Scala. Please post any errors, other than those listed below, in the book's Author Online Forum. We'll compile a comprehensive list and publish it here for everyone's convenience. Thank you!

Last updated January 8, 2015

Page xviii in About This Book:

URL is incorrectly listed as (note missing 'a'). It should be:

Page 7:

In throw new Exception, Exception should not be bold in the code listing at the top of the page.

Page 10:

URL in footnote is incorrectly listed as It should be:

Page 55:

employeesByName.get("Joe") should be lookupByName("Joe") at the top of the page.

Page 74:

Figure contains a typo:
   strean elements

should be

   stream elements

Page 82:

Int.MaxValue is incorrectly capitalized as Int.maxValue in the exercise prompt for 6.1

Page 86:

Definition of nonNegativeLessThan at bottom of page incorrectly reuses rng variable for recursive call. Definition should be:
def nonNegativeLessThan(n: Int): Rand[Int] = { rng => 
  val (i, rng2) = nonNegativeInt(rng)
  val mod = i % n
  if (i + (n-1) - mod >= 0)
    (mod, rng2)
  else nonNegativeLessThan(n)(rng2)

Pg 150, Listing 9.1:

There is a gratuitous nonstrict function argument in the definition of | in ParserOps. The definition should be:
case class ParserOps[A](p: Parser[A]) {
  def |[B>:A](p2: Parser[B]): Parser[B] = self.or(p,p2) 
  def or[B>:A](p2: Parser[B]): Parser[B] = self.or(p,p2)

Pg 150

Examples given for listOfN are incorrect. Corrected versions are:
run(listOfN(3, "ab" | "cad"))("ababcad") == Right(List("ab","ab","cad"))
run(listOfN(3, "ab" | "cad"))("cadabab") == Right(List("cad","ab","ab"))
run(listOfN(3, "ab" | "cad"))("ababab") == Right(List("ab","ab","ab"))

Pg 184:

Exercise 10.13 incorrectly defines Leaf as a case object. It should be case class:
case class Leaf[A](value: A) extends Tree[A]

Pg 187:

Definition of signature for map on Option has incorrect return type, should be:
def map[A,B](oa: Option[A])(f: A => B): Option[B]

Pg 238:

Definition of printLine has a typo, an extra Return constructor. It should be:
def printLine(s: String): IO[Unit] = Suspend(() => println(s))

Pg 240:

REPL session has a typo. It should be:
val g = List.fill(100000)(f).foldLeft(f) {
  (a, b) => x => Suspend(() => ()).flatMap { _ => a(x).flatMap(b)}
Note: we could write a little helper function to make this nicer:
def suspend[A](a: => IO[A]) = Suspend(() => ()).flatMap { _ => a }

val g = List.fill(100000)(f).foldLeft(f) {
  (a, b) => x => suspend { a(x).flatMap(b) }

Pg 241:

TrailRec should be TailRec at the top of the page.

Pg 262, List 14.3:

STArray.apply has a typo. It should be:
object STArray {
  def apply[S,A:Manifest](sz: Int, v: A): ST[S, STArray[S,A]] =
    ST(new STArray[S,A] {
      lazy val value = Array.fill(sz)(v)

Pg 275:

Await in the definition of loop is incorrect. It should be:

Pg 285:

Code snippet has an incorrectly named type parameter, currently reads:
case class Await[A,O](
req: Is[I]#f[A], recv: Either[Throwable,A] => Process[Is[I]#f,O]
) extends Process[Is[I]#f,R] // `R` should be `O`
It should read:
case class Await[A,O](
req: Is[I]#f[A], recv: Either[Throwable,A] => Process[Is[I]#f,O]
) extends Process[Is[I]#f,O]