You can do it with a recursive function and a loop:
let make_3d_matrix = (dimx, dimy, dimz) => {
let matrix = ref(Array.make(dimx, [||]));
for (i in 0 to dimx - 1) {
matrix.contents[i] = Array.make_matrix(dimy, dimz, 0);
};
matrix.contents;
};
And for higher dimensions you can nest them. I’m not sure you can make a function that returns a matrix of arbitrary nested arrays because the return type of the function cannot be different from one call to another, so any branch of the function returns array(int)
and another branch returns array(array(int))
then the compiler will say “idk what to do here bro”. So you’d need to “unify” the different things that can be returned using a variant, in which case your simple matrix becomes a type t = MatrixOfLengthN | BaseArray(array(int))
kinda thing , and then you’re returning something of type t
and of value MatrixOfLengthN(MatrixOfLengthN(BaseArray([||])))
for a 3d matrix etc… Which is probably not what you’d like. I’d recommend finding ways to avoid needing arbitrary nested arrays.