liveBook · Manning

Control flow

As explained in Functional Programming in Scala, Second Edition

Control flow is the ordering and transfer of execution among operations in a program.

Control flow can be reified as data instead of disappearing into ordinary function calls. An IO value records whether computation has finished, must perform an effect, or should continue through a result-dependent computation. section 13.3.1

An IO interpreter handles three forms of control flow:

  • Return produces a final value without further steps. section 13.3.1
  • Suspend delays an effect until interpretation forces its thunk. section 13.3.1
  • FlatMap sequences a subcomputation and passes its result to a continuation. section 13.3.1

Repeated output as data

printLine("Still going...").forever builds an infinite nested computation: suspended effects form the head, while FlatMap continuations describe the remaining control flow. Interpretation performs the effects later.

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines three constructors and a tail-recursive interpreter, including delayed evaluation and continuation handling.
Examples
Uses printLine, forever, nested FlatMap structures, and unsafeRun as concrete artifacts.
What you can do after
Build effectful computations as data and interpret them with a tail-recursive unsafeRun loop.
How it connects
Connects IO with effects, flatMap, continuations, interpreters, delayed evaluation, and tail recursion.

Also explained in

Compare how 4 books explain control flow →

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.