seq

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.

Scala: How to fill/populate a list (same element or different elements) alvin March 4, 2017 - 12:01pm

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, a simple solution is to use the fill method, like this:

scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

If you want to populate a list with different element values, another approach is to use the tabulate method:

Scala code to read a text file to an Array (or Seq)

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList
    bufferedSource.close
    lines
}

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 alvin November 27, 2016 - 4:19pm

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

How to list subdirectories beneath a directory in Scala

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 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: