partitionMap<S, T> static method

FlowHandle partitionMap<S, T>(
  1. Cell source, {
  2. required bool test(
    1. S value
    ),
  3. required T thenMap(
    1. S value
    ),
  4. required T elseMap(
    1. S value
    ),
  5. PartitionErrorHandler? onError,
})

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

  1. Each incoming pulse is type-checked.
  2. If the type matches, test is called with the payload.
  3. If test returns true, thenMap is called with the value.
  4. If test returns false, elseMap is called with the value.
  5. The result is emitted.
  6. If test, thenMap, or elseMap throws, the pulse is dropped.

Parameters:

  • source: The Source Cell. The cell providing the input values.
  • test: Predicate Function. Called with each typed payload, returns true for matched values.
  • thenMap: Matched Mapper. Called with values where test returns true.
  • elseMap: Unmatched Mapper. Called with values where test returns false.
  • 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:

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);
}