2022-07-19 18:54:22 +00:00
|
|
|
import {numComplex} from './Complex.mjs'
|
2022-07-19 16:52:16 +00:00
|
|
|
|
|
|
|
export const add = {
|
2022-07-19 18:54:22 +00:00
|
|
|
'...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)
|
2022-07-19 16:52:16 +00:00
|
|
|
}]
|
|
|
|
}
|