This is a page from my book, Functional Programming, Simplified

About the book format

Although I could easily write hundreds of pages about my consulting experiences, I want to create a book that is “accessible,” a book you can speed-read over one or two nights if you like, or perhaps read more slowly, focusing on just one chapter at a time. With this format — and with the extra “Summary” chapters at the end of the book — you can also use it as a reference, such as the night before a big meeting with a client.

Using sed to add a newline on Mac OS X

As a quick note today, I have been converting parts of the Scala Cookbook from a plain text format to a Markdown format, and as part of that I needed to add some newline characters to add spacing to the document. This wouldn’t be bad if it was a few pages, but it’s hundreds of pages, so I decided to use the Unix sed command to do the work.

How to convert a Scala collection to a String with mkString

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.29, “How to Convert a Scala Collection to a String with mkString”


You want to convert elements of a collection to a String, possibly adding a field separator, prefix, and suffix.


Use the mkString method to print a collection as a String. Given a simple collection:

How to format numbers and currency in Scala

Scala FAQ: How can I format numbers and currency in Scala, such as to control the number of decimal places and commas in the values, typically for printed output.

Basic formatting

For basic number formatting, use the f string interpolator shown in Recipe 1.4 of the Scala Cookbook, “Substituting Variables into Strings”:

scala> val pi = scala.math.Pi
pi: Double = 3.141592653589793

scala> println(f"$pi%1.5f")

A few more examples demonstrate the technique:

Scala: Displaying XML in a human-readable format (pretty printing)

Problem: You have some XML in a hard-to-read format in a Scala application, and want to print it in a format that’s easier to read, at least for humans.


Use the scala.xml.PrettyPrinter class. To see how it works, imagine starting with a long, continuous string of XML:

The Scala String format approach (and Java String.format)

Scala String formatting FAQ: How do I write code that is equivalent to the Java String.format class? That is, how do I format strings like that in Scala?

NOTE: As of Scala 2.10 this approach is a little out of date. You can still use this approach, but there's a better way to handle this situation in Scala 2.10 and newer Scala version.

In Java I always write code like this to format a String: