++

How to merge Scala sequential collections (List, Vector, ArrayBuffer, Array, Seq) alvin June 6, 2015 - 10:52am

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:

Scala has no ++ or -- operator; how to increment or decrement an integer? alvin May 31, 2015 - 3:32pm

Scala FAQ: Scala doesn't have the ++ and -- operators; are the some similar operators or methods that I can use instead?

Solution

Because val fields are immutable, they can’t be incremented or decremented, but var integer fields can be mutated with Scala’s += and −= methods:

There are no ++ or -- operators in Scala (use += or -=) alvin May 19, 2012 - 12:47pm

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator: