distinct<S> static method
Suppresses pulses that have been seen before (globally or consecutively depending on configuration).
distinct acts as a Global Duplicate Filter. It keeps track of all values that have ever been emitted and drops any value that has been seen before, regardless of position.
When to use
Use distinct when you need to ensure each value is emitted only once.
- Unique Items: Processing unique items from a stream.
- Set-Like Deduplication: Implementing set-like deduplication.
- Event Tracking: Tracking unique events.
- ID Deduplication: Deduplicating IDs.
- Data Cleaning: Removing duplicates from a stream.
How it works
- Each typed pulse's payload is extracted.
- The payload is compared to all previously seen values using
equals. - If a match is found, the pulse is dropped.
- If no match is found, the pulse passes through and the value is stored.
Memory Considerations
- O(n) Memory: The instruction stores all seen values in a list.
- Unbounded Growth: Memory grows with the number of unique values.
- Use skipRepeated when only consecutive repeats matter to avoid unbounded memory growth.
Parameters:
source: The Source Cell. The cell providing the input values.
Type Parameters:
S: The type of the input payload.
Returns:
A FlowHandle that can be used to observe the filtered values.
Example
final nums = Cell.ingress<int>();
final handle = Flow.distinct<int>(nums.cell);
nums.emit(1); // passes through
nums.emit(2); // passes through
nums.emit(1); // dropped
nums.emit(3); // passes through
nums.emit(2); // dropped
// Result: `[1, 2, 3]`
See Also:
- skipRepeated: For consecutive-only deduplication.
- filter: For general filtering.
AI Agent Guide
Layer: Flow — global dedupe operator. Not a Cell factory, not a Tissue book.
When to use: "do not flood the log" on a 15 s feed — drop any payload seen before.
When NOT to use: an ACK-resettable latch (stock Distinct has no reset() — use a custom instruction).
Live signature: Flow.distinct<S>(Cell source) — pinned at cell_flow: 1.2.0
Do NOT confuse with: Flow.skipRepeated (consecutive-only) and DistinctUntilChanged (Flow class, consecutive).
Guide: HowTo-Mitose-Flow.md §3.3 "Filter / take / skip" (#flow-filter-take-skip)
Example: example/distinct_noise_reduction_demo.dart
Cross-layer: a guarded register of seen keys is a TissueSet; the reset latch is a custom instruction.
Minimal compilable example
final src = Cell.ingress<int>();
final h = Flow.distinct<int>(src.cell);
final o = Cell.observe(source: h.cell, effect: (Pulse p) => print(p.payload));
src.emit(1);
o.stop();
Implementation
static FlowHandle distinct<S>(Cell source) {
return Distinct<S>().toHandle(source: source);
}