Skip to content

Instantly share code, notes, and snippets.

@enobufs
Last active October 27, 2018 09:01
Show Gist options
  • Save enobufs/0ac640afb9ea231b05f91a84e7a4a858 to your computer and use it in GitHub Desktop.
Save enobufs/0ac640afb9ea231b05f91a84e7a4a858 to your computer and use it in GitHub Desktop.
Scan NdArray in row-major oder.
const nj = require('numjs');
function scan(ndarr, cb) {
const args = Array(ndarr.shape.length).fill(0);
function _scan(ndarr, cb) {
const shape = ndarr.shape;
const argi = args.length - shape.length;
for (args[argi] = 0; args[argi] < shape[0]; ++args[argi]) {
if (shape.length > 1) {
const d = ndarr.pick(args[argi]);
_scan(d, cb);
} else {
cb(...args);
}
}
}
_scan(ndarr, cb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment