How to use Scala’s sortInPlaceBy method on mutable sequences (ArrayBuffer, ArrayDeque)

Table of Contents1 - Reading the Scaladoc2 - How I think sortInPlaceBy works3 - A sortInPlaceBy example4 - A second example5 - Discussion6 - Summary

When I first looked at the sortInPlaceBy method that was introduced on mutable sequences in Scala 2.13, I couldn’t figure out exactly what it was supposed to do.

Unable to find any examples of “scala sortInPlaceBy” on planet Earth this evening (February 23, 2020), I had to resort to some actual work, and looked at the Scaladoc.

Back to top

Reading the Scaladoc

This is what I see when I look at the Scaladoc for sortInPlaceBy on the ArrayBuffer:

def sortInPlaceBy[B](f: (A) => B)(implicit ord: Ordering[B]): ArrayBuffer.this.type

You can’t see by looking at that method what A is, so I scrolled up to the top of the page and saw this at the beginning of the Scaladoc:

How to create and populate Kotlin lists (initialize mutable and immutable lists)

If you ever need to create and populate a Kotlin list, I can confirm that these approaches work for an immutable and mutable lists:

// fill an immutable list
val doubles = List(5) { i -> i * 2 }

// fill a mutable list of ten elements with zeros
val ints = MutableList(10) { 0 }

The Kotlin REPL shows how these approaches work:

Kotlin functions to create Lists, Maps, and Sets

Table of Contents1 - Kotlin Arrays2 - Kotlin List functions3 - Kotlin Map functions4 - Kotlin Set functions5 - Summary: Kotlin List, Map, and Set creation functions

With Kotlin you can create lists, maps, and sets with standard functions that are automatically in scope. Here are those functions.

How to create an empty Scala Map

If you need to create an empty Map in Scala, the following examples show how to create empty immutable and mutable maps.

An empty immutable Map

You can use these approaches to create an empty immutable Scala Map:

val a = Map[Int, String]()
val a = Map.empty[Int, String]

Here’s what they look like in the REPL:

STRef, or StateRef (using a State monad)

Here’s a link to a page by James Earl Douglas that I don’t quite understand yet, but also don’t want to forget. Here’s his intro to the problem, and then the image shows his solution.

Problem: You have a mutable variable (a var in Scala) that is both read from and written to outside of a tightly-scoped block.

Solution: Remodel the block as functions that take an initial value of the variable, and return both the final value of the variable and the original return value of the expression.

What is “Functional Programming”?

Table of Contents1 - Defining “Functional Programming”2 - A working definition of “pure function”3 - Note 1: Higher-Order Functions are a great FP language feature4 - Note 2: Recursion is a by-product5 - Proof: Wikipedia’s FP definition6 - Proof: A wonderful quote from Mary Rose Cook7 - That’s great ... but why immutable values?8 - Summary9 - See also

Note: This is an excerpt from my forthcoming book on Scala and functional programming. (No one has review this text yet. All mistakes are definitely my own.)

Back to top

Defining “Functional Programming”

It’s surprisingly hard to find a consistent definition of functional programming. As just one example, some people say that functional programming (FP) is about writing pure functions — which is a good start — but then they add something else like, “The programming language must be lazy.” Really? Does a programming language really have to be lazy (non-strict) to be FP? (The correct answer is “no.”)

I share links to many definitions at the end of this lesson, but I think you can define FP with just two statements:

Scala programming best practice: Prefer immutable variables (values)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.2, “Scala programming best practice: Prefer immutable variables (values).”

Back to top


You want to reduce the use of mutable objects and data structures in your code.

Back to top


Begin with this simple philosophy, stated in the book, Programming in Scala:

“Prefer vals, immutable objects, and methods without side effects. Reach for them first.”

Table of Contents

  1. Problem
  2. Solution
Back to top