pocomath/complex/add.mjs
Glen Whitney 84a8b9d5c4 feat: Allow self-reference in implementations (#4)
This PR also uses such self-reference to define negate and add
  for Complex numbers in a way that is independent of component types.

  Also adds a bigint type and verifies that pocomath will then handle
  Gaussian integers "for free".

  Ensures that if one function is invalidated, then any that depend on it will be.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #4
2022-07-19 18:54:22 +00:00

16 lines
514 B
JavaScript

import {numComplex} from './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) => {
/* Need a "base case" to avoid infinite self-reference loops */
if (numComplex(z) && numComplex(w)) {
return {re: w.re + z.re, im: w.im + z.im}
}
return {re: ref.self(w.re, z.re), im: ref.self(w.im, z.im)}
}, seed)
}]
}