KeepLastOutput
KeepLastOutput remembers the last value produced by the inner stage and re-emits it when the inner stage
yields nothing.
The decorator has two states:
- Initial state (no value seen yet):
Yield.Noneis forwarded unchanged;Yield.Someis forwarded and transitions to the remembered state. - Remembered state: the wrapper always emits a value. A new
Yield.Somefrom the inner stage is forwarded as is and updates the remembered value; onYield.Nonethe remembered value is emitted instead.
The factory KeepLastOutput(stage) always starts in the initial state.
import h8io.stages.operators.*
import h8io.stages.projections.*
val stage = KeepLastOutput(Unlift[Int])
// stage: h8io.stages.Stage[Option[Int], Int, Nothing] = None(
// alterand = <function1>
// )
val r1 = stage(Some(7)) // inner yields Some(7): emitted, value remembered
// r1: h8io.stages.Yield[Option[Int], Int, Nothing] = Some(
// out = 7,
// status = Success,
// evolution = Mapped(
// evolution = <function1>,
// f = h8io.stages.operators.KeepLastOutput$None$$Lambda$19885/0x00007f58029b4fb0@575effc8
// )
// )
val r2 = r1.evolve()(None) // inner yields None: last value re-emitted
// r2: h8io.stages.Yield[Option[Int], Int, Nothing] = Some(
// out = 7,
// status = Success,
// evolution = Mapped(
// evolution = <function1>,
// f = h8io.stages.operators.KeepLastOutput$Some$$Lambda$19886/0x00007f58029b5380@7eb6d003
// )
// )
val r3 = r2.evolve()(Some(3)) // inner yields Some(3): emitted, remembered value updated
// r3: h8io.stages.Yield[Option[Int], Int, Nothing] = Some(
// out = 3,
// status = Success,
// evolution = Mapped(
// evolution = <function1>,
// f = h8io.stages.operators.KeepLastOutput$Some$$Lambda$19887/0x00007f58029b5750@1b4cdc85
// )
// )