From the course: Scala Essential Training for Data Science

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Mapping functions over parallel collections

Mapping functions over parallel collections - Scala Tutorial

From the course: Scala Essential Training for Data Science

Start my 1-month free trial

Mapping functions over parallel collections

- [Teacher] Let's work with functions over parallel collections. We'll start the Scala REPL. Now I want to create a value v and I'm going to set this to be a range of one to 100. I'm going to want to convert it to an array. We can convert an array into a parallel collection using the par method, and I'll call it pv for parallel version of v. And it's v.par. Now I have a parallel array. The value of pv is the same length and has the same values as v, however the pv value allows for parallel operations. Let's start by multiplying each member of the v array by two. First, I'll clear the screen. Now I have v. Now to apply an operation to every member of a collection I can use the map function. I'll use the underscore as an alias for each member of the collection. I'll say, for each member of the collection, multiply by two. That doubles everything in the array. Similarly, for parallel version, I can use the same code. I can apply the map function, use the anonymous variable, and then…

Contents