pairwise<S> static method

FlowHandle pairwise<S>(
  1. Cell source
)

Emits a tuple of the (previous, current) payloads for every pulse after the first.

pairwise acts as a Sliding Window of Size 2. It maintains a sliding window of the last two values and emits them as a pair for each consecutive pair in the stream.

When to use

Use pairwise when you need to compare adjacent values:

  • Deltas: Computing current - previous for change detection
  • Edge Detection: Detecting direction changes or state transitions
  • Trend Analysis: Comparing a reading to the one before it
  • Change Detection: Detecting when a value changes
  • Motion Detection: Detecting movement based on position changes
  • Signal Processing: Computing derivatives or differences

How it works

  1. The first typed pulse is stored and not emitted.
  2. For each subsequent pulse, the previous value and the current value are emitted as a pair (previous, current).
  3. The current value becomes the previous value for the next pair.

Parameters:

  • source: The Source Cell. The cell providing the input values.

Type Parameters:

  • S: The type of the input payload from the source cell.

Returns:

A FlowHandle that can be used to observe the emitted pairs.

Example

final ticks = Cell.ingress<int>();

final handle = Flow.pairwise<int>(ticks.cell);

ticks.emit(1); // No output (stored)
ticks.emit(2); // -> (1, 2)
ticks.emit(3); // -> (2, 3)
// Result: `[(1, 2), (2, 3)]`

See Also:

  • scan: For accumulating values over time.
  • reduce: For explicit seed accumulation.

Implementation

static FlowHandle pairwise<S>(Cell source) {
  return Pairwise<S>().toHandle(source: source);
}