reduce

Folds every cell into a single accumulator, visiting keys in ascending order and each row left to right.

import { Matrix } from "@teakit/matrix";

Matrix.reduce({ b: [3], a: [1, 2] }, (acc, value) => acc + value, 0); // 6

// Visit order is ascending key, then left-to-right within each row.
Matrix.reduce({ b: [3, 4], a: [1, 2] }, (acc, value) => [...acc, value], [] as number[]);
// [1, 2, 3, 4]

API Reference

Signature

Matrix.reduce<T, U, TKey extends string>(
  matrix: Matrix<T, TKey>,
  reducer: MatrixReducer<T, U, TKey>,
  initialValue: U,
): U;
// MatrixReducer: (accumulator, value, { key, rowIndex, colIndex }) => U

Parameters

ParameterTypeRequiredNotes
matrixMatrix<T, TKey>YesSource matrix.
reducer(acc, value, context) => UYescontext is { key, rowIndex, colIndex }.
initialValueUYesStarting accumulator.

Returns

The final accumulator value.

Throws

Does not throw (beyond errors raised by reducer).

Agent Contract

FieldValue
Kindstatic traversal
Canonical namereduce
AliasesNone
Mutates inputsNo
ReturnsU

Agent Notes

  • initialValue is required; there is no no-initial overload.
  • rowIndex follows ascending key order. Use map to produce a matrix, entries for a flat list.