-
Notifications
You must be signed in to change notification settings - Fork 305
/
NDArray.ts
25 lines (21 loc) 路 701 Bytes
/
NDArray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import deepClone from '../utils/deepClone';
class NDArray {
/**
* Initializes an N-dimensional array.
* @param {number[]} dimensions Dimensions of the array.
* @param {*} defaultValue Default value to initialize each position of the array to.
* @return {Array<any>}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static create(dimensions: Array<number>, defaultValue: any = 0): any {
let value = defaultValue;
for (let i = dimensions.length - 1; i >= 0; i--) {
/* eslint-disable no-loop-func */
value = Array(dimensions[i])
.fill(null)
.map((_) => deepClone(value));
}
return value;
}
}
export default NDArray;