liveBook · Manning

parMap

As explained in Functional Programming in Scala, Second Edition

parMap is a book-defined operation that applies a function to all elements of a list in parallel and returns a Par computation of the results.

parMap applies a function to every element of a List in parallel and returns a Par computation containing the resulting List. It separates describing the computation from running it, leaving execution to the Par abstraction. section 7.0

Basic use

val outputList = parMap(inputList)(f) section 7.0

The implementation builds the operation from existing combinators rather than treating it as a new primitive. asyncF turns f into computations for individual elements; sequence collects a List[Par[B]] into a Par[List[B]]; fork packages the combined computation for parallel execution. section 7.2.1

A compact implementation follows three steps:

  • Use sequence to transform the list of parallel computations into one Par computation of a result list. section 7.2.1
val outputList = parMap(inputList)(f)

Inspect the minimal call site for applying a function across a list. section 7.0

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Derives parMap from asyncF, sequence, and fork, stopping short of a lower-level execution analysis.
Examples
Uses a parMap call, an intermediate List[Par[B]], and a completed fork-and-sequence implementation.
What it assumes
Assumes familiarity with Par, map2, asyncF, fork, and sequence while developing sequence as the missing collection combinator.
What you can do after
Build it from scratch by composing asyncF, sequence, and fork without adding another primitive.
How it connects
Connects Par with asyncF, sequence, fork, map, map2, ExecutorService, and Future.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.