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.

A Scala method to write a list of strings to a file

As a brief note today, here’s a Scala method that writes the strings in a list — more accurately, a Seq[String] — to a file:

def writeFile(filename: String, lines: Seq[String]): Unit = {
    val file = new File(filename)
    val bw = new BufferedWriter(new FileWriter(file))
    for (line <- lines) {
        bw.write(line)
    }
    bw.close()
}
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 Seq class: methods, examples, and syntax

This page contains a large collection of examples of how to use the methods on the Scala Seq class.

Important note about Seq, IndexedSeq, and LinearSeq

As an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. As the Seq class Scaladoc states:

Scala: How to append and prepend items to Vector and Seq

Table of Contents1 - Solution2 - Example data3 - Append a single item4 - Append multiple elements5 - Prepend a single item6 - Prepend multiple elements7 - Seq works just like Vector8 - How to remember the method names9 - A possible problem

Scala FAQ: How do I append or prepend one or more elements to a Vector or Seq class?

Back to top

Solution

To append or prepend one or more elements to a Vector or Seq, use these methods:

How to get multiple, unique, random elements from a list of elements

One thing I never thought about before is that if you need to get multiple, unique, random elements from a list of elements, one solution to the problem is to shuffle the list and then take as many elements as you want/need. For instance, if you want three unique, random elements from a list of integers in Scala, you can do this:

scala> val list = List(1,2,3,4,5,1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

scala> val uniq = list.distinct
uniq: List[Int] = List(1, 2, 3, 4, 5)

scala> val shuffled = scala.util.Random.shuffle(uniq)
shuffled: List[Int] = List(1, 4, 5, 2, 3)

scala> val firstThree = shuffled.take(3)
firstThree: List[Int] = List(1, 4, 5)

As that solution shows, you start with a simple list; get the unique/distinct elements from the list; shuffle those elements to create a new list; then take the first three elements from the shuffled list. That’s probably not a great solution for huge lists, but for many simple lists it’s a way to get multiple random elements from the list.