liveBook · Manning

Eval

As explained in Functional Programming in Scala, Second Edition

Eval is a Pull data constructor that wraps an effectful computation producing a result without output elements.

Eval is a Pull constructor for embedding an effectful computation that produces a result without emitting output elements. Its type, Eval[F, R], extends Pull[F, Nothing, R], so it can appear wherever a result-producing Pull is expected. section 15.3

Interpreting an Eval

When Pull is stepped with a Monad[F], an Eval(action) case maps the action’s result into a completed computation: action.map(Left(_)). Result therefore signals completion, while Eval performs the effect needed to obtain that result. section 15.3

Eval supports several layers of the Pull and Stream interface:

  • uncons represents stepping through a Pull by wrapping its effectful step result in Eval. section 15.3
  • Stream.eval, mapEval, and unfoldEval lift effectful values or effectful generation into streams. 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]

Use the Pull definition to compare Eval with Result, Output, and FlatMap, including each constructor’s effect, output, and result types. section 15.3

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines the constructor, typing role, and step interpretation, while broader use appears through Stream operations.
Examples
Uses Eval in step and uncons, then exposes it through Stream.eval, mapEval, and unfoldEval.
What it assumes
Assumes Pull and effect type constructors, then uses Monad to interpret effectful actions.
What you can do after
Evaluate or understand how Pull interprets an effectful action as a result without emitting output.
How it connects
Connects Pull to Monad, Stream, FlatMap, Result, and Output.

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.