Matrix Multiplication in Typescript I would like some help on making my solution more functional. At present, I rely heavily on using indexes in my map functions. I am also seeking general feedback and advice, thanks.

export const getMatrixColumns = (a: number[][]) =&gt; {
  return a[0].length;
};

export const getMatrixRows = (a: number[][]) =&gt; {
  return a.length;
};

export const matricesAreValid = (a: number[][], b: number[][]) =&gt; {
  return getMatrixColumns(a) === getMatrixRows(b);
};

export const generateEmptyMatrix = (rows: number, cols: number) =&gt; {
  return [...Array(rows)].fill(0).map(() =&gt; [...Array(cols)].fill(0));
};

export const dotProduct = (a: number[], b: number[]) =&gt; {
  return a.map((value, i) =&gt; value * b[i]).reduce((acc, val) =&gt; acc + val, 0);
};
export const multiplyMatrices = (a: number[][], b: number[][]) =&gt; {
  if (matricesAreValid(a, b)) {
    const rows = getMatrixRows(a);
    const cols = getMatrixColumns(b);
    return generateEmptyMatrix(rows, cols).map((resultRow, i) =&gt;
      resultRow.map((element: number, j: number) =&gt; {
        const column = b.map(row =&gt; row[j]);
        const row = a[i];
        return dotProduct(row, column);
      })
    );
  }
};
Concise Function Body

The function body of an arrow function can be concise.

For instance can getMatrixColumns shorten to

export const getMatrixColumns = (a: number[][]) =&gt; a[0].length;




Method Name

The method names getMatrixColumns and getMatrixRows let me expect that the the methods return a number[] instead of a number. 

A better fit are getMatrixColumnLength and getMatrixRowLength.



Redundant Method Names

Each method name expect dotProduct contains some how the word matrix.

It is sufficient if the methods are grouped in a Matrix module and have names like:

// in matrix.js

export const columnLenghtOf = (a: number[][]) =&gt; 
    a[0].length;

export const rowLengthOf = (a: number[][]) =&gt; 
    a.length;

export const areValid = (a: number[][], b: number[][]) =&gt;
    columnLenghtOf(a) === columnLenghtOf(b);

/* ... */




Type Alias

Matrix could be a type alias for number[][]:

// in matrix.js

type Matrix = number[][]

export const columnLenghtOf = (a: Matrix) =&gt; 
    a[0].length;
```