This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.5, “How to Merge (Concatenate) Lists in Scala”
You want to merge/concatenate the contents of two lists.
Merge two lists using the
::: methods. Given these two lists:
This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 10.23, “How to Merge Two Sequential Collections into Pairs with the
You want to merge data from two sequential collections into a collection of key/value pairs.
zip method that’s available to Scala sequential collections to join two sequences into one:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.22, “How to Merge Scala Sequential Collections”
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.
There are a variety of solutions to this problem, depending on your needs:
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).
Array FAQ: How do I merge two Arrays or ArrayBuffers?
Solution: Use the
++ method to join two arrays into one new array:
scala> val a = Array(1,2,3) a: Array[Int] = Array(1, 2, 3) scala> val b = Array(4,5,6) b: Array[Int] = Array(4, 5, 6) scala> val c = a ++ b c: Array[Int] = Array(1, 2, 3, 4, 5, 6)
Use the same approach to merge two ArrayBuffers into a new ArrayBuffer:
Scala List FAQ: How do I merge a
List in Scala?
NOTE: I wrote the solutions shown below a long time ago, and they are not optimal. I'll update this article when I have more time. The best approach is to prepend one
List to the beginning of another
List with the
There are at least three ways to merge/concatenate Scala List instances, as shown in the examples below.
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.
AppleScript string FAQ: How do I concatenate (merge) strings in AppleScript?
Fortunately string concatenation in AppleScript is pretty easy (if not a little different). To concatenate strings in AppleScript just use the ampersand (&) operator.
Here are a few AppleScript string concatenation examples, with a dialog thrown in so you can see the result: