Best practices for matrix and N dimension array in MOTOKO
For motoko developers…
Hi, I’m Elias Cardona, from ICP Hub Latin America. I’m looking for develop and implement a nDimension matrix (array, vector, etc.) in Motoko language. Of course I’d like to do it taking advantage of the Motoko base library and it good practices.
import Nat "mo:base/Nat";
import Int "mo:base/Int";
import Array "mo:base/Array";
import Debug "mo:base/Debug";
var x : Nat= 3;
var y : Nat = 7;
actor motokosba {
let sz : Nat = 11;
func genesis(i : Nat) : Nat { i*1 };
let capaInicial : [Nat] = Array.tabulate<Nat>(sz, genesis);
let capa2 : [Nat] = Array.tabulate<Nat>(sz, genesis);
let capa3 : [Nat] = Array.tabulate<Nat>(sz, 0);
public func cmat() : () {
for(i in capaInicial.vals()) {
var it : Nat = capaInicial[i];
Debug.print(debug_show(it));
capa2.append<Nat>(capa2, it);
};
for(j in capa2.vals()) {
var itr : Nat = capa2[j];
Debug.print(debug_show(itr));
capa3.append<Nat>(capa3, itr);
};
};
// end of matrix's fill function
public func rmat() : () {
for(q in capaInicial.vals()) {
var fmt1 : Nat = capa3[q][0][0];
Debug.print(debug_show(fmt1));
for(r in capa2.vals()) {
var fmt2 : Nat = capa3[0][r][0];
Debug.print(debug_show(fmt2));
for(s in capa3.vals()) {
var fmt3 : Nat = capa3[0][0][s];
Debug.print(debug_show(fmt3));
};
};
};
};
// end of matrix's printing function
};
I leave you here the motoko playground link, that I mean, could be of support for you:
Hope someone knows which is the best way to take the idea I have in mind: implement an this data structure (‘traditional’ matrix in C/C++) in Motoko.
Regards!