Skip to content

Commit

Permalink
Replace recursive calls in typed-functions with this-style calls (#…
Browse files Browse the repository at this point in the history
…1903)

* Fix #1902: typo in an example in the documentation

* Replace recursive calls in typed-functions with `this`-style calls

* Replace more recursive calls in typed-functions with `this`-style calls

* Refactor compareNatural to use this-style recursion

Co-authored-by: josdejong <wjosdejong@gmail.com>
  • Loading branch information
nickewing and josdejong committed Jul 13, 2020
1 parent d3f7117 commit 859cfa2
Show file tree
Hide file tree
Showing 114 changed files with 803 additions and 1,022 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -32,7 +32,7 @@
"javascript-natural-sort": "^0.7.1",
"seed-random": "^2.2.0",
"tiny-emitter": "^2.1.0",
"typed-function": "^1.1.1"
"typed-function": "^2.0.0"
},
"devDependencies": {
"@babel/core": "7.10.4",
Expand Down
14 changes: 7 additions & 7 deletions src/function/algebra/derivative.js
Expand Up @@ -77,28 +77,28 @@ export const createDerivative = /* #__PURE__ */ factory(name, dependencies, ({
return options.simplify ? simplify(res) : res
},
'Node, SymbolNode': function (expr, variable) {
return derivative(expr, variable, { simplify: true })
return this(expr, variable, { simplify: true })
},

'string, SymbolNode': function (expr, variable) {
return derivative(parse(expr), variable)
return this(parse(expr), variable)
},
'string, SymbolNode, Object': function (expr, variable, options) {
return derivative(parse(expr), variable, options)
return this(parse(expr), variable, options)
},

'string, string': function (expr, variable) {
return derivative(parse(expr), parse(variable))
return this(parse(expr), parse(variable))
},
'string, string, Object': function (expr, variable, options) {
return derivative(parse(expr), parse(variable), options)
return this(parse(expr), parse(variable), options)
},

'Node, string': function (expr, variable) {
return derivative(expr, parse(variable))
return this(expr, parse(variable))
},
'Node, string, Object': function (expr, variable, options) {
return derivative(expr, parse(variable), options)
return this(expr, parse(variable), options)
}

// TODO: replace the 8 signatures above with 4 as soon as typed-function supports optional arguments
Expand Down
18 changes: 8 additions & 10 deletions src/function/algebra/rationalize.js
Expand Up @@ -125,33 +125,33 @@ export const createRationalize = /* #__PURE__ */ factory(name, dependencies, ({
* {Expression Node} node simplified expression
*
*/
const rationalize = typed(name, {
return typed(name, {
string: function (expr) {
return rationalize(parse(expr), {}, false)
return this(parse(expr), {}, false)
},

'string, boolean': function (expr, detailed) {
return rationalize(parse(expr), {}, detailed)
return this(parse(expr), {}, detailed)
},

'string, Object': function (expr, scope) {
return rationalize(parse(expr), scope, false)
return this(parse(expr), scope, false)
},

'string, Object, boolean': function (expr, scope, detailed) {
return rationalize(parse(expr), scope, detailed)
return this(parse(expr), scope, detailed)
},

Node: function (expr) {
return rationalize(expr, {}, false)
return this(expr, {}, false)
},

'Node, boolean': function (expr, detailed) {
return rationalize(expr, {}, detailed)
return this(expr, {}, detailed)
},

'Node, Object': function (expr, scope) {
return rationalize(expr, scope, false)
return this(expr, scope, false)
},

'Node, Object, boolean': function (expr, scope, detailed) {
Expand Down Expand Up @@ -643,6 +643,4 @@ export const createRationalize = /* #__PURE__ */ factory(name, dependencies, ({
} else { throw new Error('Type ' + tp + ' is not allowed') }
} // End of recurPol
} // End of polyToCanonical

return rationalize
})
22 changes: 11 additions & 11 deletions src/function/algebra/simplify.js
Expand Up @@ -154,47 +154,47 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
*/
const simplify = typed('simplify', {
string: function (expr) {
return simplify(parse(expr), simplify.rules, {}, {})
return this(parse(expr), this.rules, {}, {})
},

'string, Object': function (expr, scope) {
return simplify(parse(expr), simplify.rules, scope, {})
return this(parse(expr), this.rules, scope, {})
},

'string, Object, Object': function (expr, scope, options) {
return simplify(parse(expr), simplify.rules, scope, options)
return this(parse(expr), this.rules, scope, options)
},

'string, Array': function (expr, rules) {
return simplify(parse(expr), rules, {}, {})
return this(parse(expr), rules, {}, {})
},

'string, Array, Object': function (expr, rules, scope) {
return simplify(parse(expr), rules, scope, {})
return this(parse(expr), rules, scope, {})
},

'string, Array, Object, Object': function (expr, rules, scope, options) {
return simplify(parse(expr), rules, scope, options)
return this(parse(expr), rules, scope, options)
},

'Node, Object': function (expr, scope) {
return simplify(expr, simplify.rules, scope, {})
return this(expr, this.rules, scope, {})
},

'Node, Object, Object': function (expr, scope, options) {
return simplify(expr, simplify.rules, scope, options)
return this(expr, this.rules, scope, options)
},

Node: function (expr) {
return simplify(expr, simplify.rules, {}, {})
return this(expr, this.rules, {}, {})
},

'Node, Array': function (expr, rules) {
return simplify(expr, rules, {}, {})
return this(expr, rules, {}, {})
},

'Node, Array, Object': function (expr, rules, scope) {
return simplify(expr, rules, scope, {})
return this(expr, rules, scope, {})
},

'Node, Array, Object, Object': function (expr, rules, scope, options) {
Expand Down
6 changes: 2 additions & 4 deletions src/function/arithmetic/abs.js
Expand Up @@ -30,7 +30,7 @@ export const createAbs = /* #__PURE__ */ factory(name, dependencies, ({ typed })
* @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}
* Absolute value of `x`
*/
const abs = typed(name, {
return typed(name, {
number: absNumber,

Complex: function (x) {
Expand All @@ -47,13 +47,11 @@ export const createAbs = /* #__PURE__ */ factory(name, dependencies, ({ typed })

'Array | Matrix': function (x) {
// deep map collection, skip zeros since abs(0) = 0
return deepMap(x, abs, true)
return deepMap(x, this, true)
},

Unit: function (x) {
return x.abs()
}
})

return abs
})
14 changes: 6 additions & 8 deletions src/function/arithmetic/add.js
Expand Up @@ -57,7 +57,7 @@ export const createAdd = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y`
*/
const add = typed(name, extend({
return typed(name, extend({
// we extend the signatures of addScalar with signatures dealing with matrices

'DenseMatrix, DenseMatrix': function (x, y) {
Expand All @@ -78,17 +78,17 @@ export const createAdd = /* #__PURE__ */ factory(name, dependencies, ({ typed, m

'Array, Array': function (x, y) {
// use matrix implementation
return add(matrix(x), matrix(y)).valueOf()
return this(matrix(x), matrix(y)).valueOf()
},

'Array, Matrix': function (x, y) {
// use matrix implementation
return add(matrix(x), y)
return this(matrix(x), y)
},

'Matrix, Array': function (x, y) {
// use matrix implementation
return add(x, matrix(y))
return this(x, matrix(y))
},

'DenseMatrix, any': function (x, y) {
Expand Down Expand Up @@ -120,15 +120,13 @@ export const createAdd = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
'any, any': addScalar,

'any, any, ...any': function (x, y, rest) {
let result = add(x, y)
let result = this(x, y)

for (let i = 0; i < rest.length; i++) {
result = add(result, rest[i])
result = this(result, rest[i])
}

return result
}
}, addScalar.signatures))

return add
})
6 changes: 2 additions & 4 deletions src/function/arithmetic/addScalar.js
Expand Up @@ -17,7 +17,7 @@ export const createAddScalar = /* #__PURE__ */ factory(name, dependencies, ({ ty
* @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y`
* @private
*/
const addScalar = typed(name, {
return typed(name, {

'number, number': addNumber,

Expand All @@ -39,11 +39,9 @@ export const createAddScalar = /* #__PURE__ */ factory(name, dependencies, ({ ty
if (!x.equalBase(y)) throw new Error('Units do not match')

const res = x.clone()
res.value = addScalar(res.value, y.value)
res.value = this(res.value, y.value)
res.fixPrefix = false
return res
}
})

return addScalar
})
6 changes: 2 additions & 4 deletions src/function/arithmetic/cbrt.js
Expand Up @@ -55,7 +55,7 @@ export const createCbrt = /* #__PURE__ */ factory(name, dependencies, ({ config,
* @return {number | BigNumber | Complex | Unit | Array | Matrix}
* Returns the cubic root of `x`
*/
const cbrt = typed(name, {
return typed(name, {
number: cbrtNumber,
// note: signature 'number, boolean' is also supported,
// created by typed as it knows how to convert number to Complex
Expand All @@ -72,7 +72,7 @@ export const createCbrt = /* #__PURE__ */ factory(name, dependencies, ({ config,

'Array | Matrix': function (x) {
// deep map collection, skip zeros since cbrt(0) = 0
return deepMap(x, cbrt, true)
return deepMap(x, this, true)
}
})

Expand Down Expand Up @@ -145,6 +145,4 @@ export const createCbrt = /* #__PURE__ */ factory(name, dependencies, ({ config,
return result
}
}

return cbrt
})
6 changes: 2 additions & 4 deletions src/function/arithmetic/ceil.js
Expand Up @@ -36,7 +36,7 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
*/
const ceil = typed('ceil', {
return typed('ceil', {
number: function (x) {
if (nearlyEqual(x, round(x), config.epsilon)) {
return round(x)
Expand All @@ -63,9 +63,7 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,

'Array | Matrix': function (x) {
// deep map collection, skip zeros since ceil(0) = 0
return deepMap(x, ceil, true)
return deepMap(x, this, true)
}
})

return ceil
})
6 changes: 2 additions & 4 deletions src/function/arithmetic/cube.js
Expand Up @@ -30,7 +30,7 @@ export const createCube = /* #__PURE__ */ factory(name, dependencies, ({ typed }
* @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x Number for which to calculate the cube
* @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} Cube of x
*/
const cube = typed(name, {
return typed(name, {
number: cubeNumber,

Complex: function (x) {
Expand All @@ -47,13 +47,11 @@ export const createCube = /* #__PURE__ */ factory(name, dependencies, ({ typed }

'Array | Matrix': function (x) {
// deep map collection, skip zeros since cube(0) = 0
return deepMap(x, cube, true)
return deepMap(x, this, true)
},

Unit: function (x) {
return x.pow(3)
}
})

return cube
})
8 changes: 3 additions & 5 deletions src/function/arithmetic/divideScalar.js
Expand Up @@ -17,7 +17,7 @@ export const createDivideScalar = /* #__PURE__ */ factory(name, dependencies, ({
* @return {number | BigNumber | Fraction | Complex | Unit} Quotient, `x / y`
* @private
*/
const divideScalar = typed(name, {
return typed(name, {
'number, number': function (x, y) {
return x / y
},
Expand All @@ -38,7 +38,7 @@ export const createDivideScalar = /* #__PURE__ */ factory(name, dependencies, ({
const res = x.clone()
// TODO: move the divide function to Unit.js, it uses internals of Unit
const one = numeric(1, typeOf(y))
res.value = divideScalar(((res.value === null) ? res._normalize(one) : res.value), y)
res.value = this(((res.value === null) ? res._normalize(one) : res.value), y)
return res
},

Expand All @@ -47,14 +47,12 @@ export const createDivideScalar = /* #__PURE__ */ factory(name, dependencies, ({
res = res.pow(-1)
// TODO: move the divide function to Unit.js, it uses internals of Unit
const one = numeric(1, typeOf(x))
res.value = divideScalar(x, ((y.value === null) ? y._normalize(one) : y.value))
res.value = this(x, ((y.value === null) ? y._normalize(one) : y.value))
return res
},

'Unit, Unit': function (x, y) {
return x.divide(y)
}
})

return divideScalar
})
10 changes: 4 additions & 6 deletions src/function/arithmetic/dotDivide.js
Expand Up @@ -51,7 +51,7 @@ export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ ty
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Denominator
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x ./ y`
*/
const dotDivide = typed(name, {
return typed(name, {

'any, any': divideScalar,

Expand All @@ -73,17 +73,17 @@ export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ ty

'Array, Array': function (x, y) {
// use matrix implementation
return dotDivide(matrix(x), matrix(y)).valueOf()
return this(matrix(x), matrix(y)).valueOf()
},

'Array, Matrix': function (x, y) {
// use matrix implementation
return dotDivide(matrix(x), y)
return this(matrix(x), y)
},

'Matrix, Array': function (x, y) {
// use matrix implementation
return dotDivide(x, matrix(y))
return this(x, matrix(y))
},

'SparseMatrix, any': function (x, y) {
Expand Down Expand Up @@ -112,6 +112,4 @@ export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ ty
return algorithm14(matrix(y), x, divideScalar, true).valueOf()
}
})

return dotDivide
})

0 comments on commit 859cfa2

Please sign in to comment.