From ff2f1734c2de67cd8725af6deb52a72b611488b7 Mon Sep 17 00:00:00 2001 From: David Contreras Date: Thu, 5 Sep 2024 22:15:02 -0600 Subject: [PATCH] deepMap with number of arguments in callback --- src/expression/function/compile.js | 2 +- src/expression/function/evaluate.js | 4 ++-- src/function/arithmetic/ceil.js | 4 ++-- src/function/arithmetic/fix.js | 4 ++-- src/function/arithmetic/floor.js | 4 ++-- src/function/arithmetic/log10.js | 2 +- src/function/arithmetic/log1p.js | 2 +- src/function/arithmetic/log2.js | 2 +- src/function/arithmetic/round.js | 4 ++-- src/function/arithmetic/sign.js | 2 +- src/function/arithmetic/unaryMinus.js | 2 +- src/function/arithmetic/unaryPlus.js | 2 +- src/function/bitwise/bitNot.js | 2 +- src/function/complex/arg.js | 2 +- src/function/complex/conj.js | 2 +- src/function/complex/im.js | 2 +- src/function/complex/re.js | 2 +- src/function/logical/not.js | 2 +- src/function/probability/factorial.js | 2 +- src/function/special/erf.js | 2 +- src/function/utils/isInteger.js | 2 +- src/function/utils/isNaN.js | 2 +- src/function/utils/isNegative.js | 2 +- src/function/utils/isNumeric.js | 2 +- src/function/utils/isPositive.js | 2 +- src/function/utils/isPrime.js | 2 +- src/function/utils/isZero.js | 2 +- src/type/bigint.js | 2 +- src/type/bignumber/function/bignumber.js | 2 +- src/type/boolean.js | 2 +- src/type/complex/function/complex.js | 2 +- src/type/fraction/function/fraction.js | 2 +- src/type/number.js | 2 +- src/type/string.js | 2 +- src/type/unit/function/unit.js | 2 +- src/utils/collection.js | 28 ++++++++++++++---------- src/utils/simplifyCallback.js | 4 ++-- 37 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/expression/function/compile.js b/src/expression/function/compile.js index a0e1d2f074..d3c9f6e801 100644 --- a/src/expression/function/compile.js +++ b/src/expression/function/compile.js @@ -47,7 +47,7 @@ export const createCompile = /* #__PURE__ */ factory(name, dependencies, ({ type 'Array | Matrix': function (expr) { return deepMap(expr, function (entry) { return parse(entry).compile() - }) + }, false, 1) } }) }) diff --git a/src/expression/function/evaluate.js b/src/expression/function/evaluate.js index 7a2400d6ff..f3f687c61a 100644 --- a/src/expression/function/evaluate.js +++ b/src/expression/function/evaluate.js @@ -57,13 +57,13 @@ export const createEvaluate = /* #__PURE__ */ factory(name, dependencies, ({ typ const scope = createEmptyMap() return deepMap(expr, function (entry) { return parse(entry).compile().evaluate(scope) - }) + }, false, 1) }, 'Array | Matrix, Map | Object': function (expr, scope) { return deepMap(expr, function (entry) { return parse(entry).compile().evaluate(scope) - }) + }, false, 1) } }) }) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 4d4c7a0ea1..85f00c98f8 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -124,12 +124,12 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, self, true) + return deepMap(x, self, true, 1) }), 'Array, number | BigNumber': typed.referToSelf(self => (x, n) => { // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, i => self(i, n), true) + return deepMap(x, i => self(i, n), true, 1) }), 'SparseMatrix, number | BigNumber': typed.referToSelf(self => (x, y) => { diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index f0e8a39aaa..d32aec3d97 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -105,12 +105,12 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since fix(0) = 0 - return deepMap(x, self, true) + return deepMap(x, self, true, 1) }), 'Array | Matrix, number | BigNumber': typed.referToSelf(self => (x, n) => { // deep map collection, skip zeros since fix(0) = 0 - return deepMap(x, i => self(i, n), true) + return deepMap(x, i => self(i, n), true, 1) }), 'number | Complex | Fraction | BigNumber, Array': diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 4129d5561b..0a501d2d62 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -127,12 +127,12 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, 'Array | Matrix': typed.referToSelf(self => (x) => { // deep map collection, skip zeros since floor(0) = 0 - return deepMap(x, self, true) + return deepMap(x, self, true, 1) }), 'Array, number | BigNumber': typed.referToSelf(self => (x, n) => { // deep map collection, skip zeros since ceil(0) = 0 - return deepMap(x, i => self(i, n), true) + return deepMap(x, i => self(i, n), true, 1) }), 'SparseMatrix, number | BigNumber': typed.referToSelf(self => (x, y) => { diff --git a/src/function/arithmetic/log10.js b/src/function/arithmetic/log10.js index d96c6044a9..52d33b32b9 100644 --- a/src/function/arithmetic/log10.js +++ b/src/function/arithmetic/log10.js @@ -54,6 +54,6 @@ export const createLog10 = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/arithmetic/log1p.js b/src/function/arithmetic/log1p.js index a210750d56..ae27f4ea5f 100644 --- a/src/function/arithmetic/log1p.js +++ b/src/function/arithmetic/log1p.js @@ -59,7 +59,7 @@ export const createLog1p = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)), + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)), 'any, any': typed.referToSelf(self => (x, base) => { // calculate logarithm for a specified base, log1p(x, base) diff --git a/src/function/arithmetic/log2.js b/src/function/arithmetic/log2.js index cefd0ba2a0..0618e062c9 100644 --- a/src/function/arithmetic/log2.js +++ b/src/function/arithmetic/log2.js @@ -52,7 +52,7 @@ export const createLog2 = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) /** diff --git a/src/function/arithmetic/round.js b/src/function/arithmetic/round.js index 7e585fb5af..9bef122395 100644 --- a/src/function/arithmetic/round.js +++ b/src/function/arithmetic/round.js @@ -158,7 +158,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, 'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => { // deep map collection, skip zeros since round(0) = 0 - return deepMap(x, (value) => self(value, n, unit), true) + return deepMap(x, (value) => self(value, n, unit), true, 1) }), 'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)), @@ -167,7 +167,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, 'Array | Matrix': typed.referToSelf(self => x => { // deep map collection, skip zeros since round(0) = 0 - return deepMap(x, self, true) + return deepMap(x, self, true, 1) }), 'SparseMatrix, number | BigNumber': typed.referToSelf(self => (x, n) => { diff --git a/src/function/arithmetic/sign.js b/src/function/arithmetic/sign.js index 56a728b04e..e9b252d13e 100644 --- a/src/function/arithmetic/sign.js +++ b/src/function/arithmetic/sign.js @@ -56,7 +56,7 @@ export const createSign = /* #__PURE__ */ factory(name, dependencies, ({ typed, }, // deep map collection, skip zeros since sign(0) = 0 - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true)), + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true, 1)), Unit: typed.referToSelf(self => x => { if (!x._isDerived() && x.units[0].unit.offset !== 0) { diff --git a/src/function/arithmetic/unaryMinus.js b/src/function/arithmetic/unaryMinus.js index 98148de0bd..36aaebb103 100644 --- a/src/function/arithmetic/unaryMinus.js +++ b/src/function/arithmetic/unaryMinus.js @@ -43,7 +43,7 @@ export const createUnaryMinus = /* #__PURE__ */ factory(name, dependencies, ({ t }), // deep map collection, skip zeros since unaryMinus(0) = 0 - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true, 1)) // TODO: add support for string }) diff --git a/src/function/arithmetic/unaryPlus.js b/src/function/arithmetic/unaryPlus.js index a6b2ccf9ce..b1776c9184 100644 --- a/src/function/arithmetic/unaryPlus.js +++ b/src/function/arithmetic/unaryPlus.js @@ -55,7 +55,7 @@ export const createUnaryPlus = /* #__PURE__ */ factory(name, dependencies, ({ ty }, // deep map collection, skip zeros since unaryPlus(0) = 0 - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true)), + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true, 1)), boolean: function (x) { return numeric(x ? 1 : 0, config.number) diff --git a/src/function/bitwise/bitNot.js b/src/function/bitwise/bitNot.js index 7823834127..665a2108db 100644 --- a/src/function/bitwise/bitNot.js +++ b/src/function/bitwise/bitNot.js @@ -33,6 +33,6 @@ export const createBitNot = /* #__PURE__ */ factory(name, dependencies, ({ typed number: bitNotNumber, BigNumber: bitNotBigNumber, bigint: x => ~x, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/complex/arg.js b/src/function/complex/arg.js index 52164581b8..bc60ec17ad 100644 --- a/src/function/complex/arg.js +++ b/src/function/complex/arg.js @@ -47,6 +47,6 @@ export const createArg = /* #__PURE__ */ factory(name, dependencies, ({ typed }) // TODO: implement BigNumber support for function arg - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/complex/conj.js b/src/function/complex/conj.js index 776c14e622..852262877b 100644 --- a/src/function/complex/conj.js +++ b/src/function/complex/conj.js @@ -33,6 +33,6 @@ export const createConj = /* #__PURE__ */ factory(name, dependencies, ({ typed } return typed(name, { 'number | BigNumber | Fraction': x => x, Complex: x => x.conjugate(), - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/complex/im.js b/src/function/complex/im.js index 32c423ccc7..8e18f9e9d6 100644 --- a/src/function/complex/im.js +++ b/src/function/complex/im.js @@ -36,6 +36,6 @@ export const createIm = /* #__PURE__ */ factory(name, dependencies, ({ typed }) number: () => 0, 'BigNumber | Fraction': x => x.mul(0), Complex: x => x.im, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/complex/re.js b/src/function/complex/re.js index 380c3a32ad..c5d8e484e8 100644 --- a/src/function/complex/re.js +++ b/src/function/complex/re.js @@ -35,6 +35,6 @@ export const createRe = /* #__PURE__ */ factory(name, dependencies, ({ typed }) return typed(name, { 'number | BigNumber | Fraction': x => x, Complex: x => x.re, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/logical/not.js b/src/function/logical/not.js index 3706f00b4b..6628765c96 100644 --- a/src/function/logical/not.js +++ b/src/function/logical/not.js @@ -48,6 +48,6 @@ export const createNot = /* #__PURE__ */ factory(name, dependencies, ({ typed }) Unit: typed.referToSelf(self => x => typed.find(self, x.valueType())(x.value)), - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/probability/factorial.js b/src/function/probability/factorial.js index 081f5e702d..7eb0330b48 100644 --- a/src/function/probability/factorial.js +++ b/src/function/probability/factorial.js @@ -44,6 +44,6 @@ export const createFactorial = /* #__PURE__ */ factory(name, dependencies, ({ ty return gamma(n.plus(1)) }, - 'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self)) + 'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self, false, 1)) }) }) diff --git a/src/function/special/erf.js b/src/function/special/erf.js index 4797f529af..11cec9a5eb 100644 --- a/src/function/special/erf.js +++ b/src/function/special/erf.js @@ -53,7 +53,7 @@ export const createErf = /* #__PURE__ */ factory(name, dependencies, ({ typed }) return sign(x) * (1 - erfc3(y)) }, - 'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self)) + 'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self, false, 1)) // TODO: For complex numbers, use the approximation for the Faddeeva function // from "More Efficient Computation of the Complex Error Function" (AMS) diff --git a/src/function/utils/isInteger.js b/src/function/utils/isInteger.js index f6814d18ab..2acb1ad0c4 100644 --- a/src/function/utils/isInteger.js +++ b/src/function/utils/isInteger.js @@ -50,6 +50,6 @@ export const createIsInteger = /* #__PURE__ */ factory(name, dependencies, ({ ty return x.d === 1 && isFinite(x.n) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isNaN.js b/src/function/utils/isNaN.js index d334262a8a..c5c19e81a0 100644 --- a/src/function/utils/isNaN.js +++ b/src/function/utils/isNaN.js @@ -58,6 +58,6 @@ export const createIsNaN = /* #__PURE__ */ factory(name, dependencies, ({ typed return Number.isNaN(x.value) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isNegative.js b/src/function/utils/isNegative.js index 95126a8715..9149e2ee92 100644 --- a/src/function/utils/isNegative.js +++ b/src/function/utils/isNegative.js @@ -51,6 +51,6 @@ export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ t Unit: typed.referToSelf(self => x => typed.find(self, x.valueType())(x.value)), - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isNumeric.js b/src/function/utils/isNumeric.js index da280c6d70..2687f8bfad 100644 --- a/src/function/utils/isNumeric.js +++ b/src/function/utils/isNumeric.js @@ -38,6 +38,6 @@ export const createIsNumeric = /* #__PURE__ */ factory(name, dependencies, ({ ty return typed(name, { 'number | BigNumber | bigint | Fraction | boolean': () => true, 'Complex | Unit | string | null | undefined | Node': () => false, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isPositive.js b/src/function/utils/isPositive.js index 9ce3d3c56a..00a5bae03e 100644 --- a/src/function/utils/isPositive.js +++ b/src/function/utils/isPositive.js @@ -54,6 +54,6 @@ export const createIsPositive = /* #__PURE__ */ factory(name, dependencies, ({ t Unit: typed.referToSelf(self => x => typed.find(self, x.valueType())(x.value)), - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isPrime.js b/src/function/utils/isPrime.js index b1434ccbdd..9066bef2ca 100644 --- a/src/function/utils/isPrime.js +++ b/src/function/utils/isPrime.js @@ -126,6 +126,6 @@ export const createIsPrime = /* #__PURE__ */ factory(name, dependencies, ({ type return true }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/function/utils/isZero.js b/src/function/utils/isZero.js index deb113ff59..6714a0f125 100644 --- a/src/function/utils/isZero.js +++ b/src/function/utils/isZero.js @@ -46,6 +46,6 @@ export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed Unit: typed.referToSelf(self => x => typed.find(self, x.valueType())(x.value)), - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/type/bigint.js b/src/type/bigint.js index 2b5e90625c..cf3c7608e9 100644 --- a/src/type/bigint.js +++ b/src/type/bigint.js @@ -56,7 +56,7 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed return 0n }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) // reviver function to parse a JSON object like: diff --git a/src/type/bignumber/function/bignumber.js b/src/type/bignumber/function/bignumber.js index 49391bebaa..3aa00532f4 100644 --- a/src/type/bignumber/function/bignumber.js +++ b/src/type/bignumber/function/bignumber.js @@ -83,6 +83,6 @@ export const createBignumber = /* #__PURE__ */ factory(name, dependencies, ({ ty return new BigNumber(0) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/type/boolean.js b/src/type/boolean.js index 7ac890c994..d8e50a2c16 100644 --- a/src/type/boolean.js +++ b/src/type/boolean.js @@ -71,6 +71,6 @@ export const createBoolean = /* #__PURE__ */ factory(name, dependencies, ({ type throw new Error('Cannot convert "' + x + '" to a boolean') }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/type/complex/function/complex.js b/src/type/complex/function/complex.js index b94327e2b4..493544535a 100644 --- a/src/type/complex/function/complex.js +++ b/src/type/complex/function/complex.js @@ -89,6 +89,6 @@ export const createComplex = /* #__PURE__ */ factory(name, dependencies, ({ type throw new Error('Expected object with properties (re and im) or (r and phi) or (abs and arg)') }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/type/fraction/function/fraction.js b/src/type/fraction/function/fraction.js index 1dfb88030a..a1a7a88e7d 100644 --- a/src/type/fraction/function/fraction.js +++ b/src/type/fraction/function/fraction.js @@ -85,6 +85,6 @@ export const createFraction = /* #__PURE__ */ factory(name, dependencies, ({ typ return new Fraction(x) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/type/number.js b/src/type/number.js index 41b0864c33..876a5f0c4b 100644 --- a/src/type/number.js +++ b/src/type/number.js @@ -134,7 +134,7 @@ export const createNumber = /* #__PURE__ */ factory(name, dependencies, ({ typed return unit.toNumber(valuelessUnit) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) // reviver function to parse a JSON object like: diff --git a/src/type/string.js b/src/type/string.js index 85f57ff81c..99a4f7558f 100644 --- a/src/type/string.js +++ b/src/type/string.js @@ -50,7 +50,7 @@ export const createString = /* #__PURE__ */ factory(name, dependencies, ({ typed return x }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)), + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)), any: function (x) { return String(x) diff --git a/src/type/unit/function/unit.js b/src/type/unit/function/unit.js index bae05a6b79..ebbba43bea 100644 --- a/src/type/unit/function/unit.js +++ b/src/type/unit/function/unit.js @@ -55,6 +55,6 @@ export const createUnitFunction = /* #__PURE__ */ factory(name, dependencies, ({ return new Unit(value) }, - 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, false, 1)) }) }) diff --git a/src/utils/collection.js b/src/utils/collection.js index dafdea0646..a89f6c9ef4 100644 --- a/src/utils/collection.js +++ b/src/utils/collection.js @@ -1,8 +1,7 @@ -import { isCollection, isMatrix } from './is.js' +import { isCollection, isMatrix, isArray } from './is.js' import { IndexError } from '../error/IndexError.js' import { arraySize } from './array.js' import { _switch } from './switch.js' - /** * Test whether an array contains collections * @param {Array} array @@ -33,7 +32,7 @@ export function deepForEach (array, callback) { for (let i = 0, ii = array.length; i < ii; i++) { const value = array[i] - if (Array.isArray(value)) { + if (isArray(value)) { deepForEach(value, callback) } else { callback(value) @@ -57,13 +56,18 @@ export function deepMap (array, callback, skipZeros, numberOfArguments = 3) { if (array && isMatrix(array)) { return array.map(x => callback(x), numberOfArguments) } - 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) + return _deepMapArray(array, callback) + + function _deepMapArray (array, callback, skipZeros) { + const zerosCallback = skipZeros ? x => x === 0 ? 0 : callback(x) : callback + if (isArray(array)) { + // TODO: replace array.map with a for loop to improve performance + return array.map(function (x) { + return _deepMapArray(x, callback) + }) + } else { + return zerosCallback(array) + } } } @@ -77,7 +81,7 @@ export function deepMap (array, callback, skipZeros, numberOfArguments = 3) { * @return {Array | Matrix} res */ export function reduce (mat, dim, callback) { - const size = Array.isArray(mat) ? arraySize(mat) : mat.size() + const size = isArray(mat) ? arraySize(mat) : mat.size() if (dim < 0 || (dim >= size.length)) { // TODO: would be more clear when throwing a DimensionError here throw new IndexError(dim, size.length) @@ -102,7 +106,7 @@ function _reduce (mat, dim, callback) { let i, ret, val, tran if (dim <= 0) { - if (!Array.isArray(mat[0])) { + if (!isArray(mat[0])) { val = mat[0] for (i = 1; i < mat.length; i++) { val = callback(val, mat[i]) diff --git a/src/utils/simplifyCallback.js b/src/utils/simplifyCallback.js index ed82b66226..05f3215607 100644 --- a/src/utils/simplifyCallback.js +++ b/src/utils/simplifyCallback.js @@ -15,7 +15,7 @@ export function simplifyCallback (callback, array, name) { const firstIndex = (array.isMatrix ? array.size() : arraySize(array)).map(() => 0) const firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex) const hasSingleSignature = Object.keys(callback.signatures).length === 1 - const numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array) + const numberOfArguments = findNumberOfArguments(callback, firstValue, firstIndex, array) const simpleCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback if (numberOfArguments >= 1 && numberOfArguments <= 3) { return (...args) => _tryFunctionWithArgs(simpleCallback, args.slice(0, numberOfArguments), name, callback.name) @@ -25,7 +25,7 @@ export function simplifyCallback (callback, array, name) { return callback } -function _findNumberOfArguments (callback, value, index, array) { +export function findNumberOfArguments (callback, value, index, array) { const testArgs = [value, index, array] for (let i = 3; i > 0; i--) { const args = testArgs.slice(0, i)