map

Maps every cell into a new matrix, visiting and emitting keys in ascending order.

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

Matrix.map({ b: [1, 2], a: [3] }, (value) => value * 2);
// { a: [6], b: [2, 4] }  (keys ascending)

// The context exposes key, rowIndex (in ascending key order), and colIndex.
Matrix.map({ b: [1, 2], a: [3] }, (_value, ctx) => ctx.rowIndex);
// { a: [0], b: [1, 1] }

Matrix.map({ b: [1, 2], a: [3] }, (_value, ctx) => ctx.colIndex);
// { a: [0], b: [0, 1] }

API Reference

Signature

Matrix.map<T, U, TKey extends string>(
  matrix: Matrix<T, TKey>,
  mapper: MatrixMapper<T, U, TKey>,
): Matrix<U, TKey>;
// MatrixMapper: (value, { key, rowIndex, colIndex }) => U

Parameters

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

Returns

A new matrix with the same keys (ascending) and mapped values. The input is not mutated.

Throws

Does not throw (beyond errors raised by mapper).

Agent Contract

FieldValue
Kindstatic traversal
Canonical namemap
AliasesNone (no mapMatrix)
Mutates inputsNo
ReturnsMatrix<U, TKey>

Agent Notes

  • rowIndex follows ascending key order, not object insertion order.
  • For folding to a single value use reduce; for a flat list use entries.