Files
collection.ts/src/collection/reduceOrNull.ts

9 lines
265 B
TypeScript

export function reduceOrNull<T>(value: T[], accumulate: (acc: T, it: T) => T): T | null {
if (value.length === 0) return null;
let acc = value[0];
for (let i = 1; i < value.length; i++) {
acc = accumulate(acc, value[i]);
}
return acc;
}