How to get the first element from a Scala Set

I have no idea what I was thinking, but today I learned (or was reminded) that if you want the first element from a Scala Set you should use its head method, or headOption. For some reason I kept thinking that take should do the job, but you can see the results in the Scala REPL, where take(1) returns a Set:

Scala Vector filtering methods (examples)

This page contains a collection of examples of filtering methods that can be used with a Scala Vector class. I’ll first show a table that describes the filtering methods, and then show examples of them.

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.

How to filter a Scala Map (filterKeys, retain, transform)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.22, “How to Filter a Scala Map”


You want to filter the elements contained in a map, either by directly modifying a mutable map, or by applying a filtering algorithm on an immutable map to create a new map.

Scala List class examples: range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll share examples of the most common List operations (methods).