It won’t be long or the final version of scala 2.9.0 will be coming out. And one of the new features is “parallel collections”. It’s a really really cool feature and also very easy to use. Just by invoking the par method on a collection you can do all kind of actions like foreach, map, fold, partition etc and convert a sequential collection in a parallel one using the multi-core features of a modern processor.

Example sequential collection:

object SequentialExample extends Application {
  
  (1 to 10).foreach(x => print(" " + x))
  
}

Output:

1 2 3 4 5 6 7 8 9 10

When you execute this code, it will print 1 to 10 in a sequential manner, like you would expect.

In the parallel example I have only added the “par” method to the collection. The rest is the same.

object ParallelExample extends Application {
  
  (1 to 10).par.foreach(x => print(" " + x))
  
}

Output:

 1 6 7 8 9 10 3 4 5 2

You can see that the numbers in the output are no longer sequential. Because the load of the foreach method has been distributed over multiple cores (in parallel) you can no longer predict the sequence of the numbers.

Thoughts

For small lists with cheap calculations per entry parallel collections are probably overkill and you might introduce more overhead then gaining actually some speed. But with large lists or expensive calculations per entry you probably will see some fantastic performance gains.

Features like this make multi-core programming somewhat more accessible for the general programmer and that’s a big plus because even people who say they understand how to code with threads, the majority actually still doesn’t.

Links
Download scala 2.9.0 RC1
Official Scala documentation of collections