feat: Switch to function-based specification of dependencies
Allows dependencies to be economically expressed and used. For example, see the new definition of subtract. Credit for the basic idea goes to James Drew, see https://stackoverflow.com/a/41525264 Resolves #21.
This commit is contained in:
parent
d72c443616
commit
9fb3aa2959
13 changed files with 104 additions and 102 deletions
|
@ -1,13 +1,10 @@
|
|||
export {Types} from './Types/Complex.mjs'
|
||||
|
||||
export const add = {
|
||||
'...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)
|
||||
}
|
||||
'...Complex': ({self}) => addends => {
|
||||
if (addends.length === 0) return {re:0, im:0}
|
||||
const seed = addends.shift()
|
||||
return addends.reduce(
|
||||
(w,z) => ({re: self(w.re, z.re), im: self(w.im, z.im)}), seed)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ export const complex = {
|
|||
* have a numeric/scalar type, e.g. by implementing subtypes in
|
||||
* typed-function
|
||||
*/
|
||||
'any, any': {does: (x, y) => ({re: x, im: y})},
|
||||
'any, any': () => (x, y) => ({re: x, im: y}),
|
||||
/* Take advantage of conversions in typed-function */
|
||||
Complex: {does: z => z}
|
||||
Complex: () => z => z
|
||||
}
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
export {Types} from './Types/Complex.mjs'
|
||||
|
||||
export const negate = {
|
||||
Complex: {
|
||||
uses: ['self'],
|
||||
does: ref => z => {
|
||||
return {re: ref.self(z.re), im: ref.self(z.im)}
|
||||
}
|
||||
}
|
||||
Complex: ({self}) => z => ({re: self(z.re), im: self(z.im)})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue