3 Answers
3

You can use Scala Parallel Collections. They are currently a part of Scala nightly releases, and will be released in Scala 2.9. The idea is that most operations available in regular collections are parallelized, so that parallel collections can be used in the same way.

Currently, there are a few collection types available - parallel ranges, parallel arrays and parallel hash tries. For instance, you can invoke a parallel map and fold operations on a parallel array like this:

There are other parallel collection operations available as well. Note that fold must take an associative operator - in the example above, addition is associative ((A + B) + C == A + (B + C)), i.e. you can add subsequences of numbers in any order and you will always obtain the same sum (reduce has a similar contract).

One other thing to be aware of is that the closures passed to parallel collections are invoked simultaneously. If they have side-effects, such as modifying a local variable in the environment, these accesses have to be synchronized. For instance, you could do something like this: