collections

Think of the Scala collections’ map method as “transform”

I’ve written this before, but when I saw this “pseudocode to Scala code” example in the book Functional Thinking, I thought it was worth mentioning again: If you have trouble grokking the Scala map method, think of it as being named transform instead. It transforms an input collection to an output collection, based on the algorithm you supply.

For those coming from the OOP world, I think “transform” is a better word because it is more meaningful, at least initially.

Links about the Scala 2.13 collections classes alvin August 11, 2019 - 9:15am

Related to some work/research I’m currently doing I thought I’d share these links about the Scala 2.13 collections classes:

This older link is also good:

Kotlin collections methods: examples and syntax alvin November 14, 2018 - 10:23am

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")
Recently-added Scala cheat sheets, tutorials, syntax, and examples alvin June 17, 2018 - 6:10pm

As I try to organize things a bit around here, here’s a list of some tutorials I’ve written lately about the Scala collections classes:

Scala 2.13 Collections Feature Freeze alvin June 12, 2018 - 11:51am

Here’s a note about the Scala 2.13 Collections Feature Freeze., which includes a link to this post on how/why the Scala 2.13 collections were redesigned from scratch.

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 alvin April 30, 2018 - 10:21am

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 alvin November 18, 2017 - 4:10pm

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 alvin February 15, 2017 - 8:02pm
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: