Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -834,3 +834,21 @@ const random = sample(numbers);

assert(numbers.includes(random as number));
```

### 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:

```ts
import { runningReduce } from "https://deno.land/std@$STD_VERSION/collections/mod.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]);
```
1 change: 1 addition & 0 deletions collections/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export * from "./first_not_nullish_of.ts";
export * from "./drop_last_while.ts";
export * from "./reduce_groups.ts";
export * from "./sample.ts";
export * from "./running_reduce.ts";
27 changes: 27 additions & 0 deletions collections/running_reduce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

/**
* 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/mod.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) => O,
initialValue: O,
): O[] {
let currentResult = initialValue;
return array.map((el) => currentResult = reducer(currentResult, el));
}
43 changes: 43 additions & 0 deletions collections/running_reduce_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../testing/asserts.ts";
import { runningReduce } from "./running_reduce.ts";

Deno.test({
name: "[collections/runningReduce] no mutation",
fn() {
const numbers = [1, 2, 3, 4, 5];
runningReduce(numbers, (sum, current) => sum + current, 0);

assertEquals(numbers, [1, 2, 3, 4, 5]);
},
});

Deno.test({
name: "[collections/runningReduce] array of numbers",
fn() {
const numbers = [1, 2, 3, 4, 5];
const result = runningReduce(numbers, (sum, current) => sum + current, 0);

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

Deno.test({
name: "[collections/runningReduce] array of strings",
fn() {
const strings = ["a", "b", "c", "d", "e"];
const result = runningReduce(strings, (str, current) => str + current, "");

assertEquals(result, ["a", "ab", "abc", "abcd", "abcde"]);
},
});

Deno.test({
name: "[collections/runningReduce] empty input",
fn() {
const result = runningReduce([], (sum, current) => sum + current, 0);

assertEquals(result, []);
},
});