mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
module.exports = function (math) {
|
|
var error = require('../../util/error'),
|
|
isMatrix = require('../../type/Matrix').isMatrix;
|
|
|
|
/**
|
|
* Execute a callback method on each entry of the matrix or the array.
|
|
* @param {Matrix/array} x The container to iterate on.
|
|
* @param {function} callback The callback method is invoked with three
|
|
* parameters: the value of the element, the index
|
|
* of the element, and the Matrix/array being traversed.
|
|
*/
|
|
math.forEach = function (x, callback) {
|
|
if (arguments.length != 2) {
|
|
throw new error.ArgumentsError('forEach', arguments.length, 2);
|
|
}
|
|
|
|
if (Array.isArray(x)) {
|
|
return _forEachArray(x, callback);
|
|
} else if (isMatrix(x)) {
|
|
return x.forEach(callback);
|
|
} else {
|
|
throw new error.UnsupportedTypeError('forEach', x);
|
|
}
|
|
};
|
|
|
|
function _forEachArray (array, callback) {
|
|
var index = [];
|
|
var recurse = function (value, dim) {
|
|
if (Array.isArray(value)) {
|
|
value.forEach(function (child, i) {
|
|
index[dim] = i; // zero-based index
|
|
recurse(child, dim + 1);
|
|
});
|
|
}
|
|
else {
|
|
callback(value, index, array);
|
|
}
|
|
};
|
|
recurse(array, 0);
|
|
};
|
|
|
|
}; |