feat(Chain): add computation pipelines like mathjs

Also adds a `mean()` operation so there will be at least one operation
  that takes only a rest parameter, to exercise the ban on splitting
  such a parameter between the stored value and new arguments.
  Adds various tests of chains.

  Resolves #32.
This commit is contained in:
Glen Whitney 2022-08-01 16:24:20 -07:00
parent 46ae7f78ab
commit 814f660000
7 changed files with 100 additions and 14 deletions

View file

@ -82,4 +82,15 @@ describe('The default full pocomath instance "math"', () => {
math.complex(11n, -4n))
assert.strictEqual(math.negate(math.complex(3n, 8n)).im, -8n)
})
it('creates chains', () => {
const mychain = math.chain(7).negate()
assert.strictEqual(mychain.value, -7)
mychain.add(23).sqrt().lcm(10)
assert.strictEqual(mychain.value, 20)
assert.strictEqual(math.mean(3,4,5), 4)
assert.throws(() => math.chain(3).mean(4,5), /chain function.*split/)
assert.throws(() => math.chain(3).foo(), /Unknown operation/)
})
})

View file

@ -17,7 +17,9 @@ describe('A custom instance', () => {
it("works when partially assembled", () => {
bw.install(complex)
// Not much we can call without any number types:
assert.deepStrictEqual(bw.complex(0, 3), {re: 0, im: 3})
const i3 = {re: 0, im: 3}
assert.deepStrictEqual(bw.complex(0, 3), i3)
assert.deepStrictEqual(bw.chain(0).complex(3).value, i3)
// Don't have a way to negate things, for example:
assert.throws(() => bw.negate(2), TypeError)
})
@ -40,6 +42,7 @@ describe('A custom instance', () => {
assert.strictEqual(pm.subtract(5, 10), -5)
assert.strictEqual(pm.floor(3.7), 3)
assert.throws(() => pm.floor(10n), TypeError)
assert.strictEqual(pm.chain(5).add(7).value, 12)
pm.install(complexAdd)
pm.install(complexNegate)
pm.install(complexComplex)
@ -52,6 +55,9 @@ describe('A custom instance', () => {
assert.deepStrictEqual(
pm.floor(math.complex(1.9, 0)),
math.complex(1))
// And the chain functions refresh themselves:
assert.deepStrictEqual(
pm.chain(5).add(pm.chain(0).complex(7).value).value, math.complex(5,7))
})
it("can defer definition of (even used) types", () => {