arraybuffer

Hello, Scala: A Scala tutorial alvin December 18, 2017 - 10:17am

As I mentioned last night, I’m creating a website named Hello, Scala (hello-scala.com) as a simple introduction and tutorial for people interested in the Scala programming language.

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 (syntax) alvin January 21, 2017 - 1:36pm

As a quick note, this is the syntax for creating a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

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

The key thing to know is that the keyword new is not required before the ArrayBuffer. (This is because ArrayBuffer is either defined as a case class, or because it has an apply method defined. I haven’t looked at its source code to know which approach is taken.)

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 inner classes in Scala alvin June 13, 2015 - 3:02pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.16, “How to create inner classes in Scala.”

Problem

You want to create a Scala class as an inner class to help keep the class out of your public API, or to otherwise encapsulate your code.

Solution

Declare one class inside another class. In the following example, a case class named Thing is declared inside of a class named PandorasBox:

How to create a Scala “for comprehension” (for/yield loop) alvin June 7, 2015 - 3:18pm

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 (multi-dimensional arrays) 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) alvin June 7, 2015 - 2:59pm

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.