partition<S> static method
Routes pulses based on whether they satisfy the test predicate.
partition transforms each value into a Split record containing
both the original value and a boolean indicating whether it passed
the predicate.
When to use
Use partition when you need to tag values with match information without separating them.
- Categorization: Tagging items as matching or not.
- Match Context: Preserving match information for downstream.
- Conditional Processing: Processing based on match status.
- Classification: Classifying items into two categories.
- Filtering by Status: Filtering based on match status.
How it works
- Each incoming pulse is type-checked.
- If the type matches,
testis called with the payload. - The result and value are wrapped in a
Splitrecord. - The
Splitrecord is emitted. - If
testthrows an error, the pulse is dropped.
Non‑obvious
- No State: No state is maintained between pulses.
- Per-Item Emit: Emits one
Splitfor each input. - Predicate Evaluation:
testis called for each value. - Type Safety: Generic over value type
S.
Parameters:
source: The Source Cell. The cell providing the input values.test: Predicate Function. Called with each typed payload, returnstrueif the value matches the condition.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 partitioned values.
Example
final input = Cell.ingress<int>();
final handle = Flow.partition<int>(
input.cell,
test: (n) => n.isEven,
);
input.emit(1); // -> Split(matched: false, value: 1)
input.emit(2); // -> Split(matched: true, value: 2)
See Also:
- partitionMap: For mapping matched/unmatched values differently.
- partitionOnly: For filtering by match status.
Implementation
static FlowHandle partition<S>(
Cell source, {
required bool Function(S value) test,
PartitionErrorHandler? onError,
}) {
return Partition<S>(test, onError: onError).toHandle(source: source);
}