Scala collections, and life in a parallel universe (a quick look)

If you ever want to have a little fun with Scala, try experimenting with the Scala parallel collections. The following example gives you a little idea of what can happen in the most simple case of creating and using a parallel collection:

scala> import scala.collection.parallel.immutable.ParVector
import scala.collection.parallel.immutable.ParVector

scala> val x = ParVector.range(1, 20)
x: scala.collection.parallel.immutable.ParVector[Int] = 
   ParVector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

scala> x.foreach(println)
10
11
12
13
14
15
16
17
18
19
1
2
5
6
7
8
9
3
4

One look at that output will tell you you're not in a single-threaded universe any more.

Depending on your needs, parallel collections can be a great benefit. (Or, if you were expecting output in a linear order, an, er, non-benefit.)

For more information on Scala's parallel collections, check out these links: