partition<S> static method

FlowHandle partition<S>(
  1. Cell source, {
  2. required bool test(
    1. S value
    ),
  3. PartitionErrorHandler? onError,
})

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

  1. Each incoming pulse is type-checked.
  2. If the type matches, test is called with the payload.
  3. The result and value are wrapped in a Split record.
  4. The Split record is emitted.
  5. If test throws an error, the pulse is dropped.

Non‑obvious

  • No State: No state is maintained between pulses.
  • Per-Item Emit: Emits one Split for each input.
  • Predicate Evaluation: test is 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, returns true if 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:

Implementation

static FlowHandle partition<S>(
  Cell source, {
  required bool Function(S value) test,
  PartitionErrorHandler? onError,
}) {
  return Partition<S>(test, onError: onError).toHandle(source: source);
}