reduce()
reduce: accumulate over the whole source, emit ONE final value on the source’s COMPLETE
(RxJS reduce). Unlike {@link scan} (which emits every step), reduce stays quiet until the
source terminates. completeWhenDepsComplete:false + terminalAsRealInput:true: the fn fires on
the source COMPLETE (reading terminal===true) and emits the accumulator + COMPLETE. An empty
source emits the seed (RxJS parity). A source ERROR auto-forwards (errorWhenDepsError default).
Import
Section titled “Import”import { reduce } from "@graphrefly/ts/operators";Signature
Section titled “Signature”function reduce( reducer: (acc: T, v: S) => T, seed: T,): Operator<S, T>Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
reducer | (acc: T, v: S) => T | Reducer called with the accumulator and each upstream DATA value. |
seed | T | Initial accumulator value emitted if the source completes empty. |
Returns
Section titled “Returns”An operator that emits one accumulated value when the source completes.
Example
Section titled “Example”import { graph } from "@graphrefly/ts/graph";import { reduce } from "@graphrefly/ts/operators";
const total = graph().initNode(reduce((sum: number, value: number) => sum + value, 0), [source]);Source
Section titled “Source”packages/ts/src/graph/operators.ts