Summary: This short post shows a few examples of using parallel collections in Scala.
To be clear, these examples of using Scala parallel collections aren’t my own examples, they come from this page on the scala-lang.org website. But, for the completeness of my Scala cookbook recipes, I wanted to make sure I included a reference to parallel collections here. (I do have other examples of using parallel collections in Scala on this site.)
First, here’s an example of converting a normal List to a parallel list (technically a
ParSeq) so you can then run a parallel
map method to transform a collection of
String objects to all-uppercase strings:
scala> val lastNames = List("Smith","Jones","Frankenstein","Bach","Jackson","Rodin").par lastNames: scala.collection.parallel.immutable.ParSeq[String] = ParVector(Smith, Jones, Frankenstein, Bach, Jackson, Rodin) scala> lastNames.map(_.toUpperCase) res0: scala.collection.parallel.immutable.ParSeq[String] = ParVector(SMITH, JONES, FRANKENSTEIN, BACH, JACKSON, RODIN)
And here’s a second example of how to convert a
map operation on a
List from the usual approach:
val list = (1 to 10000).toList list.map(_ + 42)
to using the
map method on a parallel collection by again invoking
.par, this time without creating the explicit intermediate reference (as was done with
lastNames in the previous example):
list.par.map(_ + 42)
Again, these Scala parallel programming examples come from this page on the official Scala website. I’ll add my own examples here in time, but until I get a chance to write those, I wanted to make sure that people who are using my Scala cookbook as a reference had a link to a great parallel programming resource.
Also, here’s a link to a great Measuring Performance article on the official Scala website.
This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.