reduceWhile
Like reduce but stops early when the predicate returns false.
The predicate receives the current accumulator and element. When it
returns false, the current accumulator is returned without processing
the remaining elements.
Usage
reduceWhile<T, R>(array: readonly T[], predicate: (accumulator: R, value: T) => boolean, fn: (accumulator: R, value: T, index: number) => R, initial: R)
Like reduce but stops early when the predicate returns false.
The predicate receives the current accumulator and element. When it
returns false, the current accumulator is returned without processing
the remaining elements.
Parameters
arrayreadonly T[]required
the source array.
predicate(accumulator: R, value: T) => booleanrequired
the condition used to select behavior.
fn(accumulator: R, value: T, index: number) => Rrequired
the function to apply.
initialRrequired
the initial accumulator value.
Returns
the accumulated value when the input ends or the predicate first returns false.