liveBook · Manning

choiceMap

As explained in Functional Programming in Scala, Second Edition

choiceMap is a parallel-computation combinator that selects a computation from a map using the result of an initial computation.

choiceMap selects a parallel computation from a Map using the result of an initial parallel computation. It first runs key, waits for the resulting key, looks up choices(k), and runs the selected Par.

The combinator illustrates a progression from specialized selection to a general data-dependent computation:

  • choice handles a Boolean by selecting between two computations. section 7.4
  • choiceMap replaces indexed lookup with key-based lookup, while still requiring only a function from the initial result to the next computation. section 7.4

Map-directed execution

def choiceMap[K, V](key: Par[K])(choices: Map[K, Par[V]]): Par[V] = es => val k = key.run(es).get choices(k).run

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines the executor-level implementation and relates Map lookup to the more general chooser abstraction.
Examples
Uses a Map[K, Par[V]] whose key is produced by an initial Par.
What it assumes
Assumes familiarity with Par computations, executors, and running computations to obtain results.
What you can do after
Apply it via Par to select and run a computation according to a computed Map key.
How it connects
Connects choice, choiceN, chooser, Par, and data-dependent computation.

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.