liveBook · Manning

Effect type

As explained in Functional Programming in Scala, Second Edition

An effect type is a type-level representation of computations that may perform specified effects while producing values.

An effect type represents computations that may perform specified effects while producing values. Parameterizing Pull and Stream with F[_] keeps those effects explicit instead of hiding them inside one monolithic I/O wrapper. section 15.3

Pull makes the design concrete: Result produces a final value, Output emits a value, Eval wraps an action of type F[R], and FlatMap composes computations. Its step operation interprets these cases through a Monad[F]. section 15.3

Pure and effectful streams

Stream(1, 2, 3).repeat.take(5).toList yields List(1, 2, 3, 1, 2) as a pure Stream[Nothing, Int]. The same stream can be viewed as Stream[IO, Int], producing an IO[List[Int]]. section 15.3

enum Pull[+F[_], +O, +R]:
  case Result[+R](result: R) extends Pull[Nothing, Nothing, R]
  case Output[+O](value: O) extends Pull[Nothing, O, Unit]
  case Eval[+F[_], R](action: F[R]) extends Pull[F, Nothing, R]
  case FlatMap[+F[_], X, +O, +R](
    source: Pull[F, O, X], f: X => Pull[F, O, R]) extends Pull[F, O, R]

Inspect the complete effect-parameterized Pull definition, including covariant parameters, Eval, and FlatMap. section 15.3

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Descends from constructors through variance, step, folding, and effectful unfolding.
Examples
Uses Eval, pure repeated streams, IO-valued streams, and effectful unfolding.
What it assumes
Assumes familiarity with Scala type parameters and Monad-based effect operations.
What you can do after
Build it from scratch using effect-parameterized Pull and Stream definitions.
How it connects
Connects Pull and Stream with Monad, variance, FlatMap, and IO.

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.