A stateful API produces behavior or results that depend on state maintained across interactions. Purely functional design preserves the behavior while making state transitions explicit: each operation receives a state and returns both a result and the next state. section 6.0 section 6.3
Making state explicit changes how callers compose operations:
- Replace hidden mutation with an API that computes the next state. section 6.3
- Pass the returned state into the next operation instead of reusing the old state. section 6.3
- Factor repeated state-passing into abstractions such as Rand and State, with map, map2, flatMap, and sequence. section 6.8
Generating two random values
A naive implementation calls rng.nextInt twice and discards both updated generators, so it repeats the same state transition. The pure version binds the first result with rng2, calls rng2.nextInt, and returns ((i1, i2), rng3). The caller now owns the updated state and can continue the computation deterministically. section 6.3
trait Foo: def bar: (Bar, Foo) def baz: (Int, Foo)
Contrasts a class that hides mutable state with a functional interface whose methods return both a value and the next state. section 6.3













