deno.land / std@0.166.0 / collections / running_reduce.ts

running_reduce.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * 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: * * ```ts * import { runningReduce } from "https://deno.land/std@$STD_VERSION/collections/running_reduce.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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]); * ``` */export function runningReduce<T, O>( array: readonly T[], reducer: (accumulator: O, current: T, currentIndex: number) => O, initialValue: O,): O[] { let currentResult = initialValue; return array.map((el, currentIndex) => currentResult = reducer(currentResult, el, currentIndex) );}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago