liveBook · Manning

Bind

As explained in Functional Programming in Scala, Second Edition

Bind is a computation combinator that runs a computation and feeds its result to a function producing the next computation.

Bind runs an initial parallel computation, then uses its result to generate the next computation. The second computation need not exist until that result is available, so the operation is more general than choosing among preexisting containers or branches. section 7.4

From choice to flatMap

A conditional computation can be generalized from choice to chooser: pa.flatMap(f) runs pa, then applies f to its result to obtain the next Par computation. The next computation is therefore constructed from the first result rather than selected from a fixed collection. section 7.4

extension [A](pa: Par[A]) def flatMap[B](f: A => Par[B]): Par[B]

Inspect the flatMap signature that expresses bind for parallel computations. section 7.4

Related algebraic exercises include:

  • Use join to implement flatMap by mapping into a nested Par and flattening it. section 7.4
  • Use flatMap to implement join, then express map2 using flatMap and unit. section 7.4

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains the signature, operational meaning, and decomposition into map plus join, then stops at implementation exercises.
Examples
Uses choice, chooser, nested Par values, and implementation challenges involving join, flatMap, and map2.
What it assumes
Assumes parallel computations represented as Par values and familiarity with mapping computations into nested computations.
What you can do after
Build from scratch: implement join and derive flatMap or map2 from the parallel-computation primitives.
How it connects
Connects bind with flatMap, join, map, map2, unit, and conditional computation combinators.

Also explained in

Compare how 5 books explain bind →

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.