arraybuffer

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.

How to create a Scala ArrayBuffer

As a quick note, this is how you create a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

var fruits = ArrayBuffer[String]()
var ints = ArrayBuffer[Int]()

The key there is that the keyword new is not required before the ArrayBuffer.

While I’m in the neighborhood, here are some other ways you can work with ArrayBuffer:

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 create a Scala “for comprehension” (for/yield loop)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.4, “How to create a "for comprehension" (for/yield loop).”

Problem

You want to create a new collection from an existing collection by applying an algorithm (and potentially one or more guards) to each element in the original collection.

Solution

Use a yield statement with a for loop and your algorithm to create a new collection from an existing collection.

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 sort Arrays in Scala alvin June 6, 2015 - 12:59pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes in the book, Recipe 11.10, “How to Sort Arrays in Scala”

Problem

You want to sort the elements in an Array (or ArrayBuffer).

How to delete Array and ArrayBuffer elements in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.9, “How to Delete Array and ArrayBuffer Elements in Scala”

Problem

You want to delete elements from an Array or ArrayBuffer.

Solution

An ArrayBuffer is a mutable sequence, so you can delete elements with the usual -=, --=, remove, and clear methods.

You can remove one or more elements with -=:

How to create an Array whose size can change (ArrayBuffer) alvin June 6, 2015 - 12:52pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one the shortest recipes in the book, Recipe 11.8, “How to Create an Array Whose Size Can Change (ArrayBuffer)”

Problem

You want to create an array whose size can change, i.e., a completely mutable array.

Solution

An Array is mutable in that its elements can change, but its size can’t change. To create a mutable, indexed sequence whose size can change, use the ArrayBuffer class.

How to sort Scala collections classes (sortWith, sorted, Ordered, Ordering) alvin June 6, 2015 - 12:05pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.28, “How to Sort a Scala Collection”

Problem

You want to sort a sequential collection. Or, you want to implement the Ordered trait in a custom class so you can use the sorted method, or operators like <, <=, >, and >= to compare instances of your class.