pairwise<S> static method
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 - previousfor 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
- The first typed pulse is stored and not emitted.
- For each subsequent pulse, the previous value and the current value
are emitted as a pair
(previous, current). - 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:
Implementation
static FlowHandle pairwise<S>(Cell source) {
return Pairwise<S>().toHandle(source: source);
}