Methods on the Scala collections classes, organized by category

When I wrote the Scala Cookbook, I gave each recipe and then each chapter my full attention. I thought that if I wrote each recipe as well as possible, and included important recipes in each chapter, well, I wanted each chapter to be worth the price of the entire book. That was my goal.

As a result of this effort -- and perhaps to the chagrin of my editor -- the Scala collections chapters ended up being 130 pages in length.

Scala: When the sum of a list is a very large number

As a brief note, I knew that the sum function I wrote in my book on Scala and functional programming would return a wrong value if the sum of the integers in a list was greater than Int.MaxValue, and I was curious how the built-in sum method on lists handled the same problem. It turns out that it works in the same way.

So, if you need to sum a list of integers that may exceed Int.MaxValue, you might need to write a sum function that calculates and returns the sum as a Long. (The same is true for Float and Double.)

Scala: How to fill/populate a list (same element or different elements)

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, one solution is to use the fill method that’s available to Scala sequences, like this:

scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

If you want to populate a list with different element values, another approach is to use the tabulate method:

Scala code to read a text file to an Array (or Seq)

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList

How to shuffle (randomize) a list in Scala

As a quick note today, to shuffle/randomize a list in Scala, use this technique:


Here’s what this approach looks like in the Scala REPL:

Scala ‘for loop’ examples and syntax

Table of Contents1 - Example data structures2 - Basic for-loop examples3 - Using generators in for-loops4 - Scala for-loop generators with guards5 - Scala for/yield examples (for-expressions)6 - Scala for-loop counters (and zip, zipWithIndex)7 - Using a for loop with a Scala Map8 - Multiple futures in a for loop9 - foreach examples10 - Summary

Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples.

This page is a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala for loop examples — technically called a for-comprehension or for-expression — I hope these examples are helpful.



How (and why) to make immutable collections covariant

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.5, “How (and why) to make immutable collections covariant.”


You want to create a collection whose elements can’t be changed (they’re immutable), and want to understand how to specify it.

How to write a Scala method that takes a simple generic type

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 19.2, “How to write a Scala method that takes a simple generic type.”


You’re not concerned about type variance, and want to create a Scala method (or function) that takes a generic type, such as a method that accepts a Seq[A] parameter.