Skip to main content

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.None is forwarded unchanged; Yield.Some is forwarded and transitions to the remembered state.
  • Remembered state: the wrapper always emits a value. A new Yield.Some from the inner stage is forwarded as is and updates the remembered value; on Yield.None the 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$28778/0x00007fea1f5b2620@2aebd1de
// )
// )
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$28779/0x00007fea1f5b29f0@66ed0aa0
// )
// )
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$28780/0x00007fea1f5b2dc0@2b77f7e7
// )
// )