fix: Separate typed instance for each PocomathInstance (#15)

Also starts each PocomathInstance with no types at all, and uses the new
  situation to eliminate the need for a Complex "base case".

  Resolves #14.
  Resolves #13.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #15
This commit is contained in:
Glen Whitney 2022-07-22 20:49:14 +00:00
parent ed71b15969
commit 0069597a76
25 changed files with 120 additions and 74 deletions

View file

@ -1,5 +1,5 @@
import assert from 'assert'
import math from '../pocomath.mjs'
import math from '../src/pocomath.mjs'
describe('The default full pocomath instance "math"', () => {
it('can subtract numbers', () => {

View file

@ -1,5 +1,5 @@
import assert from 'assert'
import PocomathInstance from '../PocomathInstance.mjs'
import PocomathInstance from '../../src/core/PocomathInstance.mjs'
describe('PocomathInstance', () => {
it('creates an instance that can define typed-functions', () => {

View file

@ -1,26 +1,26 @@
import assert from 'assert'
import math from '../pocomath.mjs'
import typed from 'typed-function'
import PocomathInstance from '../PocomathInstance.mjs'
import * as numbers from '../number/all.mjs'
import * as numberAdd from '../number/add.mjs'
import * as complex from '../complex/all.mjs'
import * as complexAdd from '../complex/add.mjs'
import * as complexNegate from '../complex/negate.mjs'
import extendToComplex from '../complex/extendToComplex.mjs'
import math from '../src/pocomath.mjs'
import PocomathInstance from '../src/core/PocomathInstance.mjs'
import * as numbers from '../src/number/all.mjs'
import * as numberAdd from '../src/number/add.mjs'
import * as complex from '../src/complex/all.mjs'
import * as complexAdd from '../src/complex/add.mjs'
import * as complexNegate from '../src/complex/negate.mjs'
import extendToComplex from '../src/complex/extendToComplex.mjs'
const bw = new PocomathInstance('backwards')
describe('A custom instance', () => {
it("works when partially assembled", () => {
bw.install(complex)
assert.deepStrictEqual(bw.add(2, bw.complex(0, 3)), {re: 2, im: 3})
assert.deepStrictEqual(bw.negate(2), bw.complex(-2,-0))
assert.deepStrictEqual(bw.subtract(2, bw.complex(0, 3)), {re: 2, im: -3})
// Not much we can call without any number types:
assert.deepStrictEqual(bw.complex(0, 3), {re: 0, im: 3})
// Don't have a way to negate things, for example:
assert.throws(() => bw.negate(2), TypeError)
})
it("can be assembled in any order", () => {
bw.install(numbers)
typed.addConversion({from: 'string', to: 'number', convert: x => +x})
bw.install({Types: {string: {test: s => typeof s === 'string'}}})
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
assert.strictEqual(bw.negate('8'), -8)
assert.deepStrictEqual(bw.add(bw.complex(1,3), 1), {re: 2, im: 3})
@ -36,7 +36,7 @@ describe('A custom instance', () => {
assert.strictEqual(pm.subtract(5, 10), -5)
pm.install(complexAdd)
pm.install(complexNegate)
// Should be enough to allow complex subtraction, as subtract is generic
// Should be enough to allow complex subtraction, as subtract is generic:
assert.deepStrictEqual(
pm.subtract({re:5, im:0}, {re:10, im:1}), {re:-5, im: -1})
})
@ -45,8 +45,9 @@ describe('A custom instance', () => {
const cherry = new PocomathInstance('cherry')
cherry.install(numberAdd)
await extendToComplex(cherry)
// Now we have an instance that supports addition for number and complex
// and little else:
/* Now we have an instance that supports addition for number and complex
and little else:
*/
assert.strictEqual(cherry.add(3, 4, 2), 9)
assert.deepStrictEqual(
cherry.add(cherry.complex(3, 3), 4, cherry.complex(2, 2)),