std/collections/mod.ts

Deno standard library
Go to Latest
function runningReduce
import { runningReduce } from "https://deno.land/std@0.139.0/collections/mod.ts?s=runningReduce";

Calls the given reducer on each element of the given collection, passing it's result as the accumulator to the next respective call, starting with the given initialValue. Returns all intermediate accumulator results.

Example:

import { runningReduce } from "https://deno.land/std@0.139.0/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@0.139.0/testing/asserts.ts";

const numbers = [1, 2, 3, 4, 5];
const sumSteps = runningReduce(numbers, (sum, current) => sum + current, 0);

assertEquals(sumSteps, [1, 3, 6, 10, 15]);

Parameters

array: readonly T[][src]
reducer: (
accumulator: O,
current: T,
currentIndex: number,
) => O
[src]
initialValue: O[src]