append

How to merge Scala sequential collections (List, Vector, ArrayBuffer, Array, Seq)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.22, “How to Merge Scala Sequential Collections”

Problem

You want to join two sequences into one sequence, either keeping all of the original elements, finding the elements that are common to both collections, or finding the difference between the two sequences.

Solution

There are a variety of solutions to this problem, depending on your needs:

Make the Scala Vector class your default immutable sequence

This is an excerpt from the Scala Cookbook. This is Recipe 10.7, “Make the Vector Class Your ‘Go To’ Immutable Sequence.”

Problem

You want a fast, general-purpose, immutable, sequential collection type for your Scala applications.

Solution

The Vector class was introduced in Scala 2.8 and is now considered to be the “go to,” general-purpose immutable sequential data structure.

Scala List class examples: range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll try to share examples of the most common List operations (methods).

Vim insert commands

Vim FAQ: What is the Vim insert command?

Short answer: Type the letter 'i' while in Vim command mode. You'll then be able to type text into your document, right where the cursor is currently positioned.

Vim insert commands

Longer answer: There are several other variations of the Vim insert command you can use:

How to concatenate strings in Perl

A Perl FAQ is "How do you concatenate (merge) two or more strings in Perl?"

Use the "." operator

The short answer is that you use the . operator. Here's a simple example:

$name = "alvin" . " " . "alexander";

Of course I could have also done that like this:

$name = "alvin " . "alexander";

but I wanted to show an example with more than two strings.

How to append text to a file with Ruby

Ruby file FAQ: How do I append text to a file in Ruby?

Solution: Appending text to a file with Ruby is similar to other languages: you need to open the file in "append" mode, write your data, and then close the file.

Here's a quick example that demonstrates how to append "Hello, world" to a file named myfile.out in the current directory:

How to append text to a file with the Java FileWriter class

Java file writing FAQ: How do I append text to the end of a text file in Java?

The short answer is that you should create a FileWriter instance with the append flag set to true, like this:

BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

The rest of this article explains this.