Skip to content

Instantly share code, notes, and snippets.

@khafatech
Created December 3, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khafatech/b496f4f6ed10e3decc703b69a826d22c to your computer and use it in GitHub Desktop.
Save khafatech/b496f4f6ed10e3decc703b69a826d22c to your computer and use it in GitHub Desktop.
(
var numRows = 3;
var numCols = 4;
// a = Array.geom(numRows*numCols,1,3);
a = Array.series(numRows*numCols, start: 1, step: 1);
/*
[ [ 1, 2, 3, 4, 5 ],
[ 6, 7, 8, 9, 10 ],
[ 11, 12, 13, 14, 15 ]]
*/
// 3 rows, 4 cols
b = Array2D.fromArray(numRows, numCols, a);
// throws error
// b[0];
b[0,0]; // 1st element
b[1,2]; // 2nd row, 3rd col
b[1,5]; // 10th element, not error
// or, create an Array of Array's
// c is not an Array2D
c = a.reshape(numRows, numCols);
c[0]; // first row - an array
c[0][0]; // 1st element
c[1][2]; // 2nd row, 3rd col
// the comma syntax doesn't work; returns
c[1,5]; // 2nd row
// over the column limit nil
c[1][5];
// over the row limit.
// c[100] returns nil
// The 2nd subscript, [5], tries to subscript nil and throws error
// c[100][5];
// last element
c[numRows-1][numCols-1];
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment