From 81ff04f88f96ea59e6715154b0753b97447871bf Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Fri, 22 Jul 2022 21:48:06 -0700 Subject: [PATCH] feat: Additional notations for specifying dependencies and implementations Allow just a plain function for when there are no dependencies, and allow an object with 'uses' and 'does' properties for the dependencies (optional) and implementation, respectively. --- src/complex/add.mjs | 15 +++++++++------ src/complex/complex.mjs | 4 ++-- src/complex/negate.mjs | 9 ++++++--- src/core/PocomathInstance.mjs | 22 ++++++++++++++++++++-- src/number/add.mjs | 2 +- src/number/negate.mjs | 2 +- 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/complex/add.mjs b/src/complex/add.mjs index 3583b38..d44d3fd 100644 --- a/src/complex/add.mjs +++ b/src/complex/add.mjs @@ -1,10 +1,13 @@ export {Types} from './Types/Complex.mjs' export const add = { - '...Complex': [['self'], ref => addends => { - if (addends.length === 0) return {re:0, im:0} - const seed = addends.shift() - return addends.reduce((w,z) => - ({re: ref.self(w.re, z.re), im: ref.self(w.im, z.im)}), seed) - }] + '...Complex': { + uses: ['self'], + does: ref => addends => { + if (addends.length === 0) return {re:0, im:0} + const seed = addends.shift() + return addends.reduce((w,z) => + ({re: ref.self(w.re, z.re), im: ref.self(w.im, z.im)}), seed) + } + } } diff --git a/src/complex/complex.mjs b/src/complex/complex.mjs index c86c3e8..9a5cee9 100644 --- a/src/complex/complex.mjs +++ b/src/complex/complex.mjs @@ -5,7 +5,7 @@ export const complex = { * have a numeric/scalar type, e.g. by implementing subtypes in * typed-function */ - 'any, any': [[], (x, y) => ({re: x, im: y})], + 'any, any': {does: (x, y) => ({re: x, im: y})}, /* Take advantage of conversions in typed-function */ - Complex: [[], z => z] + Complex: {does: z => z} } diff --git a/src/complex/negate.mjs b/src/complex/negate.mjs index bf4e612..9cb21c6 100644 --- a/src/complex/negate.mjs +++ b/src/complex/negate.mjs @@ -1,7 +1,10 @@ export {Types} from './Types/Complex.mjs' export const negate = { - Complex: [['self'], ref => z => { - return {re: ref.self(z.re), im: ref.self(z.im)} - }] + Complex: { + uses: ['self'], + does: ref => z => { + return {re: ref.self(z.re), im: ref.self(z.im)} + } + } } diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 3093291..fcb86f9 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -70,7 +70,12 @@ export default class PocomathInstance { for (const func in this._imps) { if (func === 'Types') continue for (const definition of Object.values(this._imps[func])) { - for (const dependency of definition[0]) { + let deps = [] + if (Array.isArray(definition)) deps = definition[0] + else if (typeof definition === 'object') { + deps = definition.uses || deps + } + for (const dependency of deps) { const depName = dependency.split('(',1)[0] if (doneSet.has(depName)) continue requiredSet.add(depName) @@ -158,7 +163,20 @@ export default class PocomathInstance { this._ensureTypes() const tf_imps = {} for (const signature in imps) { - const [deps, imp] = imps[signature] + const specifier = imps[signature] + let deps = [] + let imp + if (typeof specifier === 'function') { + imp = specifier + } else if (Array.isArray(specifier)) { + [deps, imp] = specifier + } else if (typeof specifier === 'object') { + deps = specifier.uses || deps + imp = specifier.does + } else { + throw new SyntaxError( + `Cannot interpret signature definition ${specifier}`) + } if (deps.length === 0) { tf_imps[signature] = imp } else { diff --git a/src/number/add.mjs b/src/number/add.mjs index 70be9bb..d47b10e 100644 --- a/src/number/add.mjs +++ b/src/number/add.mjs @@ -1,5 +1,5 @@ export {Types} from './Types/number.mjs' export const add = { - '...number': [[], addends => addends.reduce((x,y) => x+y, 0)], + '...number': addends => addends.reduce((x,y) => x+y, 0), } diff --git a/src/number/negate.mjs b/src/number/negate.mjs index aaf8bc0..bdfa823 100644 --- a/src/number/negate.mjs +++ b/src/number/negate.mjs @@ -1,3 +1,3 @@ export { Types } from './Types/number.mjs' -export const negate = {number: [[], n => -n]} +export const negate = {number: n => -n}