partitionMap<S, T> static method
Map matched / unmatched with different projectors.
partitionMap is similar to partition but instead of emitting
a Split record, it applies different mapping functions to
matched and unmatched values.
When to use
Use partitionMap when you need to transform matched and unmatched values differently.
- Conditional Formatting: Formatting values based on predicate.
- Different Processing: Processing matched/unmatched differently.
- Type Conversion: Converting to different types based on status.
- Labeling: Labeling values based on match status.
- Data Enrichment: Enriching values differently based on status.
How it works
- Each incoming pulse is type-checked.
- If the type matches,
testis called with the payload. - If
testreturnstrue,thenMapis called with the value. - If
testreturnsfalse,elseMapis called with the value. - The result is emitted.
- If
test,thenMap, orelseMapthrows, the pulse is dropped.
Parameters:
source: The Source Cell. The cell providing the input values.test: Predicate Function. Called with each typed payload, returnstruefor matched values.thenMap: Matched Mapper. Called with values wheretestreturnstrue.elseMap: Unmatched Mapper. Called with values wheretestreturnsfalse.onError: Error Handler. Optional callback for handling errors.
Type Parameters:
S: The type of the input payload.T: The type of the output payload.
Returns:
A FlowHandle that can be used to observe the mapped values.
Example
final input = Cell.ingress<int>();
final handle = Flow.partitionMap<int, String>(
input.cell,
test: (n) => n.isEven,
thenMap: (n) => 'even-$n',
elseMap: (n) => 'odd-$n',
);
input.emit(1); // -> odd-1
input.emit(2); // -> even-2
See Also:
- partition: For tagging values with match status.
- partitionOnly: For filtering by match status.
Implementation
static FlowHandle partitionMap<S, T>(
Cell source, {
required bool Function(S value) test,
required T Function(S value) thenMap,
required T Function(S value) elseMap,
PartitionErrorHandler? onError,
}) {
return PartitionMap<S, T>(
test,
thenMap: thenMap,
elseMap: elseMap,
onError: onError,
).toHandle(source: source);
}