(mShape, fill)
| 84 | * @returns {Matrix} |
| 85 | */ |
| 86 | export const generate = (mShape, fill) => { |
| 87 | /** |
| 88 | * Generates the matrix recursively. |
| 89 | * |
| 90 | * @param {Shape} recShape - the shape of the matrix to generate |
| 91 | * @param {CellIndices} recIndices |
| 92 | * @returns {Matrix} |
| 93 | */ |
| 94 | const generateRecursively = (recShape, recIndices) => { |
| 95 | if (recShape.length === 1) { |
| 96 | return Array(recShape[0]) |
| 97 | .fill(null) |
| 98 | .map((cellValue, cellIndex) => fill([...recIndices, cellIndex])); |
| 99 | } |
| 100 | const m = []; |
| 101 | for (let i = 0; i < recShape[0]; i += 1) { |
| 102 | m.push(generateRecursively(recShape.slice(1), [...recIndices, i])); |
| 103 | } |
| 104 | return m; |
| 105 | }; |
| 106 | |
| 107 | return generateRecursively(mShape, []); |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * Generates the matrix of zeros of specified shape. |
no test coverage detected