MCPcopy
hub / github.com/immutable-js/immutable-js / reduce

Function reduce

src/CollectionHelperMethods.ts:4–27  ·  view source on GitHub ↗
(
  collection: C,
  reducer: (reduction: V | R, value: V, key: K, iter: C) => R,
  reduction: V | R | undefined,
  context: unknown,
  useFirst: boolean,
  reverse: boolean
)

Source from the content-addressed store, hash-verified

2import assertNotInfinite from './utils/assertNotInfinite';
3
4export function reduce<K, V, R, C extends Collection<K, V>>(
5 collection: C,
6 reducer: (reduction: V | R, value: V, key: K, iter: C) => R,
7 reduction: V | R | undefined,
8 context: unknown,
9 useFirst: boolean,
10 reverse: boolean
11): V | R | undefined {
12 // @ts-expect-error Migrate to CollectionImpl in v6
13 assertNotInfinite(collection.size);
14 // @ts-expect-error Migrate to CollectionImpl in v6
15 collection.__iterate((v: V, k: K, c: C) => {
16 if (useFirst) {
17 useFirst = false;
18 reduction = v;
19 } else {
20 // `reduction` has already been seeded here (either with the provided
21 // initial value or with the first iterated value), so it is never the
22 // `undefined` placeholder — only a `V` or a `R`.
23 reduction = reducer.call(context, reduction!, v, k, c);
24 }
25 }, reverse);
26 return reduction;
27}
28
29export function keyMapper<K, V>(v: V, k: K): K {
30 return k;

Callers 2

reduceFunction · 0.90
reduceRightFunction · 0.90

Calls 2

assertNotInfiniteFunction · 0.85
__iterateMethod · 0.45

Tested by

no test coverage detected