runningReduce
import { runningReduce } from "jsr:@ende/collections";function
function runningReduce<T, O>(array: readonly T[], reducer: (accumulator: O, current: T, currentIndex: number) => O, initialValue: O): O[]Calls the given reducer on each element of the given collection, passing its result as the accumulator to the next respective call, starting with the given initialValue. Returns all intermediate accumulator results.
array: readonly T[]The array to reduce.
reducer: (accumulator: O, current: T, currentIndex: number) => OThe reducer function to apply to each element.
initialValue: OThe initial value of the accumulator.
Returns
An array of all intermediate accumulator results.
Example: Basic usage
import { runningReduce } from "@ende/collections/running-reduce";
import { assertEquals } from "@ende/assert";
const numbers = [1, 2, 3, 4, 5];
const sumSteps = runningReduce(numbers, (sum, current) => sum + current, 0);
assertEquals(sumSteps, [1, 3, 6, 10, 15]);