mathjs/lib/utils/collection/deepMap.js
2015-07-18 13:22:38 +02:00

26 lines
840 B
JavaScript

'use strict';
/**
* Execute the callback function element wise for each element in array and any
* nested array
* Returns an array with the results
* @param {Array | Matrix} array
* @param {Function} callback The callback is called with two parameters:
* value1 and value2, which contain the current
* element of both arrays.
* @param {boolean} [skipZeros] Invoke callback function for non-zero values only.
*
* @return {Array | Matrix} res
*/
module.exports = function deepMap(array, callback, skipZeros) {
if (array && (typeof array.map === 'function')) {
// TODO: replace array.map with a for loop to improve performance
return array.map(function (x) {
return deepMap(x, callback, skipZeros);
});
}
else {
return callback(array);
}
};