sequence

How to drop the first matching element in a Scala sequence

Summary: This blog post shows one way to drop/filter the first matching element from a Scala sequence (Seq, List, Vector, Array, etc.). I don’t claim that the algorithm is efficient, but it does work.

Background

While creating some Scala test code earlier today I had an immutable list of toppings for a pizza, and I got into a situation where I wanted to remove the first instance of a topping.

A Scala ‘foldLeft’ function written using recursion alvin February 23, 2017 - 2:07pm

As a short note, here’s some Scala source code that shows how to write a foldLeft function using recursion:

How to shuffle (randomize) a list in Scala

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

scala.util.Random.shuffle(List(1,2,3,4))

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 - Generators in for loops4 - for loop generators with guards5 - Scala for/yield examples6 - Scala for loop counters (and zip, zipWithIndex)7 - Using a for loop with a 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 currently 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 — I hope these examples are helpful.

How to list subdirectories beneath a directory in Scala alvin June 19, 2015 - 11:13am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.10, “How to list subdirectories beneath a directory in Scala.”

Problem

You want to generate a list of subdirectories in a given directory.

Solution

Use a combination of the Java File class and Scala collection methods:

How to use a Scala ‘for’ loop with embedded ‘if’ statements (guards)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.3, “How to use a 'for' loop with embedded 'if' statements (guards).”

Problem

You want to add one or more conditional clauses to a for loop, typically to filter out some elements in a collection while working on the others.

Solution

Add an if statement after your generator, like this:

How to use Scala ‘for’ loops (expressions) with multiple counters alvin June 7, 2015 - 3:07pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.2, “How to use Scala for loops with multiple counters.”

Problem

You want to create a loop with multiple counters, such as when iterating over a multi‐dimensional array.

Solution

You can create a for loop with two counters like this:

Scala: How to loop over a collection with ‘for’ and ‘foreach’ (plus for loop translation)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.1, “How to loop over a collection with for and foreach (and how a for loop is translated)”

Problem

You want to iterate over the elements in a collection, either to operate on each element in the collection, or to create a new collection from the existing collection.

How to merge (concatenate) Lists in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.5, “How to Merge (Concatenate) Lists in Scala”

Problem

You want to merge/concatenate the contents of two lists.

Solution

Merge two lists using the ++, concat, or ::: methods. Given these two lists:

How to populate Scala collections with a Range

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.25, “How to Populate a Scala Collection with a Range”

Problem

You want to populate a Scala List, Array, Vector, or other sequence with a Range.

Solution

Call the range method on sequence classes that support it, or create a Range and convert it to the desired sequence.