From 8db830c31db2ff7c7b644fdb3fe65f315433410b Mon Sep 17 00:00:00 2001 From: Guillermo Indalecio Fernandez Date: Tue, 22 Oct 2013 10:48:11 +0200 Subject: [PATCH] Added full reduce capabilities not tested --- lib/type/collection.js | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/lib/type/collection.js b/lib/type/collection.js index 21962aa88..21e93134b 100644 --- a/lib/type/collection.js +++ b/lib/type/collection.js @@ -146,6 +146,64 @@ exports.deepMap2 = function deepMap2(array1, array2, callback) { return res; }; +/** + * Reduce the given array to a scalar, applying a callback function to + * each pair of values, in the selected dimension. + * @param {Array | Matrix} mat + * @param {Integer} dimension + * @param {function} callback + * @return {Array | Matrix} res + */ +exports.reduce = function reduce (mat, dim, callback) { + if (mat instanceof Matrix) { + mat = mat.valueOf(); + } + return _reduce(mat, dim, callback); +} + +function _reduce(mat, dim, callback){ + if(dim<=0){ + if( typeof mat[0] === "number" ){ + var i; + var val = mat[0]; + for(i=0; i