collections

Kotlin collections methods: examples and syntax

As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.

Sample data

First, here’s some sample data:

val a = listOf(10, 20, 30, 40, 10)
val names = listOf("joel", "ed", "chris", "maurice")

Scala 2.13.0-M4 release notes (collections changes)

Under the covers — and sometimes above the covers — Scala is changing. These notes about Scala 2.13.0-M4 describe some of the changes coming to the Scala collections classes.

A few things not shown in the image are:

  • The scala-xml library is no longer bundled with the release
  • Procedure syntax (def m() { ... }) is deprecated
  • View bound syntax (A <% B) is deprecated
  • Assorted deprecated methods and classes have been removed

See the Scala 2.13.0-M4 release notes for more details.

An Illustrated Guide to Covariance and Contravariance

From the article I linked to: “Generics can often seem confusing. How often have you started to solve a problem with generics, only to realize that they don’t quite work like you thought they did? The good news is that there are some simple, foundational concepts that underpin generic variance. And once you understand those concepts, you won’t have to memorize acronyms or resort to trial-and-error - you’ll simply understand how and why they work.”

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.

Starting to write an immutable singly-linked list in Scala

Table of Contents1 - Background: What is a Cons cell?2 - What it might look like in Scala3 - Starting to create my own Cons class4 - My second effort5 - Defining my nil value6 - Defining Cons7 - Replacing the NilCons method bodies8 - Adding a toString method to Cons9 - The complete code at this point10 - I’d really like a :: method11 - Interested?12 - See also

For some examples in my new book on functional programming in Scala I needed to create a collection class of some sort. Conceptually an immutable, singly-linked list is relatively easy to grok, so I decided to create my own Scala list from scratch. This tutorial shows how I did that.

Back to top

Background: What is a Cons cell?

The first time I learned about linked lists was in a language named Lisp. In Lisp, a linked list is created as a series of “Cons” cells. A cons cell is simple, it contains only two things:

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.”

Problem

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