partitionOnly<S> static method
Keep only the matching side.
partitionOnly is a filtering operator that only passes values that match the predicate.
When to use
Use partitionOnly when you only want values that match (or don't match) a predicate.
- Filtering: Filtering values by a condition.
- Positive Filtering: Keeping only matching values.
- Negative Filtering: Keeping only non-matching values.
- Validation: Passing only valid values.
- Data Cleaning: Keeping only clean data.
How it works
- Each incoming pulse is type-checked.
- If the type matches,
testis called with the payload. - If
testreturnsmatched(defaulttrue), the value is emitted. - If
testreturns the opposite, the value is dropped. - If
testthrows an error, the pulse is dropped.
Parameters:
source: The Source Cell. The cell providing the input values.test: Predicate Function. Called with each typed payload, returnstruefor values to be considered.matched: Filter Mode. Iftrue, keeps values wheretestreturnstrue. Iffalse, keeps values wheretestreturnsfalse. Defaults totrue.onError: Error Handler. Optional callback for handling errors.
Type Parameters:
S: The type of the input payload.
Returns:
A FlowHandle that can be used to observe the filtered values.
Example
final input = Cell.ingress<int>();
final handle = Flow.partitionOnly<int>(
input.cell,
test: (n) => n.isEven,
);
input.emit(1); // dropped
input.emit(2); // -> 2
input.emit(3); // dropped
input.emit(4); // -> 4
See Also:
- partition: For tagging values with match status.
- partitionMap: For mapping matched/unmatched values differently.
Implementation
static FlowHandle partitionOnly<S>(
Cell source, {
required bool Function(S value) test,
bool matched = true,
PartitionErrorHandler? onError,
}) {
return PartitionOnly<S>(
test,
matched: matched,
onError: onError,
).toHandle(source: source);
}