From a22add9434e4416cadabfbdb8990eebc705ff8cf Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 18 Jul 2022 19:25:58 -0700 Subject: [PATCH 1/2] feat: add number negate and roll up number functions into one module --- number/all.mjs | 2 ++ number/negate.mjs | 3 +++ pocomath.mjs | 4 ++-- test/_pocomath.mjs | 4 ++++ 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 number/all.mjs create mode 100644 number/negate.mjs diff --git a/number/all.mjs b/number/all.mjs new file mode 100644 index 0000000..21a270a --- /dev/null +++ b/number/all.mjs @@ -0,0 +1,2 @@ +export {add} from './add.mjs' +export {negate} from './negate.mjs' diff --git a/number/negate.mjs b/number/negate.mjs new file mode 100644 index 0000000..b9a73cb --- /dev/null +++ b/number/negate.mjs @@ -0,0 +1,3 @@ +export const negate = { + number: [[], n => -n] +} diff --git a/pocomath.mjs b/pocomath.mjs index c8f5b18..3c04c11 100644 --- a/pocomath.mjs +++ b/pocomath.mjs @@ -1,8 +1,8 @@ /* Core of pocomath: generates the default instance */ import PocomathInstance from './PocomathInstance.mjs' -import * as numberAdd from './number/add.mjs' +import * as numbers from './number/all.mjs' const math = new PocomathInstance('math') -math.install(numberAdd) +math.install(numbers) export default math diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index 756a8c0..d1be328 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -7,4 +7,8 @@ describe('The default full pocomath instance "math"', () => { assert.strictEqual(math.add(1.5, 2.5, 3.5), 7.5) assert.strictEqual(math.add(Infinity), Infinity) }) + it('can negate numbers', () => { + assert.strictEqual(math.negate(-1), 1) + assert.strictEqual(math.add(10, math.negate(3)), 7) + }) }) -- 2.34.1 From b16af5cd71fad4356f443ae8b7f96d4d582b98d4 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 18 Jul 2022 20:04:27 -0700 Subject: [PATCH 2/2] feat: Allow nonrecursive whole-function dependencies and use to subtract --- PocomathInstance.mjs | 25 +++++++++++++++++++++++-- generic/subtract.mjs | 3 +++ number/all.mjs | 1 + test/_pocomath.mjs | 5 +++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 generic/subtract.mjs diff --git a/PocomathInstance.mjs b/PocomathInstance.mjs index a28744d..3ca4385 100644 --- a/PocomathInstance.mjs +++ b/PocomathInstance.mjs @@ -74,17 +74,38 @@ export default class PocomathInstance { throw new SyntaxError(`No implementations for ${name}`) } const tf_imps = {} - // TODO: handle nonempty dependencies for (const signature in imps) { const [deps, imp] = imps[signature] if (deps.length === 0) { tf_imps[signature] = imp } else { - throw new Error('Unimplemented') + const refs = {} + for (const dep of deps) { + // TODO: handle self dependencies + if (dep.slice(0,4) === 'self') { + throw new Error('self-reference unimplemented') + } + // TODO: handle signature-specific dependencies + if (dep.includes('(')) { + throw new Error('signature specific reference unimplemented') + } + refs[dep] = this._ensureBundle(dep) // just assume acyclic for now + } + tf_imps[signature] = imp(refs) } } const tf = typed(name, tf_imps) this[name] = tf return tf } + + /** + * Ensure that the generated typed function is assigned to the given + * name and return it + */ + _ensureBundle(name) { + const maybe = this[name] + if (typed.isTypedFunction(maybe)) return maybe + return this._bundle(name) + } } diff --git a/generic/subtract.mjs b/generic/subtract.mjs new file mode 100644 index 0000000..8a90697 --- /dev/null +++ b/generic/subtract.mjs @@ -0,0 +1,3 @@ +export const subtract = { + 'any,any': [['add', 'negate'], ref => (x,y) => ref.add(x, ref.negate(y))] +} diff --git a/number/all.mjs b/number/all.mjs index 21a270a..395eb54 100644 --- a/number/all.mjs +++ b/number/all.mjs @@ -1,2 +1,3 @@ export {add} from './add.mjs' export {negate} from './negate.mjs' +export {subtract} from '../generic/subtract.mjs' diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index d1be328..2f2a0f2 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -2,11 +2,16 @@ import assert from 'assert' import math from '../pocomath.mjs' describe('The default full pocomath instance "math"', () => { + it('can subtract numbers', () => { + assert.strictEqual(math.subtract(12, 5), 7) + }) + it('can add numbers', () => { assert.strictEqual(math.add(3, 4), 7) assert.strictEqual(math.add(1.5, 2.5, 3.5), 7.5) assert.strictEqual(math.add(Infinity), Infinity) }) + it('can negate numbers', () => { assert.strictEqual(math.negate(-1), 1) assert.strictEqual(math.add(10, math.negate(3)), 7) -- 2.34.1