From d8199341e0c9a360bff95201e09cee15c561a6eb Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 16 Oct 2023 18:48:26 -0700 Subject: [PATCH] refactor: Settle on making all implementations (possibly vacuous) factories --- src/Complex/arithmetic.ts | 6 +++++- src/index.ts | 8 -------- src/interfaces/type.ts | 18 ++++++------------ src/numbers/arithmetic.ts | 33 +++++++++++++++++++-------------- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/Complex/arithmetic.ts b/src/Complex/arithmetic.ts index d2155d1..0f11f2e 100644 --- a/src/Complex/arithmetic.ts +++ b/src/Complex/arithmetic.ts @@ -97,4 +97,8 @@ export const sqrt = const denom = dep.conservativeSqrt(denomsq) return dep.divideReal(num, denom) } -$reflect!([sqrt]) + +$reflect!([ + add, addReal, unaryMinus, conj, subtract, multiply, absquare, divideReal, + reciprocal, divide, sqrt +]) diff --git a/src/index.ts b/src/index.ts index 0952d95..e3377e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,7 @@ import {inspect} from 'node:util' import {Dispatcher} from './core/Dispatcher.js' -import {Signatures, Deps} from './interfaces/type.js' -import {$$typeToString} from 'ts-macros' import * as Specifications from './all.js' -class Placeholder {} -const allSignatures = - $$typeToString!>>(true, false, true) - -console.log('Found signatures', allSignatures) - export default new Dispatcher(Specifications) import {Complex} from './Complex/type.js' diff --git a/src/interfaces/type.ts b/src/interfaces/type.ts index 446c761..3ab1b9a 100644 --- a/src/interfaces/type.ts +++ b/src/interfaces/type.ts @@ -74,13 +74,12 @@ export interface Signatures { type SignatureKey = keyof Signatures -export type Signature, T> = - Signatures[Name] -export type DSignature, T> = - Signatures[Name] & {reflectedType?: string, actualType?: Signatures[Name]} -export type Returns, T> = ReturnType[Name]> -export type Deps = T extends unknown ? { [K in keyof T]: T[K] } : never; -export type Dependencies, T> = Deps<{[K in Name]: Signature}> +export type Signature, T> = Signatures[Name] +export type Returns, T> = + ReturnType[Name]> +type Deps = T extends unknown ? { [K in keyof T]: T[K] } : never; +export type Dependencies, T> = + Deps<{[K in Name]: Signature}> export type AliasOf = T & {aliasOf?: Name} @@ -89,8 +88,3 @@ export function $reflect(tup: ImplTuple) { +[[tup], (elt: T) => elt.reflectedType = $$typeToString!(true, false, true)] } -export function $Dreflect(tup: ImplTuple) { - +[[tup], (elt: T) => - elt.reflectedType = $$typeToString!(true, false, true)] -} -export type RTT = {reflectedType?: string} diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index 354667d..51d2ce5 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,18 +1,20 @@ import type {configDependency} from '../core/Config.js' -import type {Dependencies, Signature, RTT, DSignature} from '../interfaces/type.js' -import {$reflect, $Dreflect} from '../interfaces/type.js' +import type {Dependencies, Signature} from '../interfaces/type.js' +import {$reflect} from '../interfaces/type.js' -export const add: Signature<'add', number> & RTT = (a, b) => a + b -export const unaryMinus: Signature<'unaryMinus', number> = a => -a -export const conj: DSignature<'conj', number> = a => a -export const subtract = (): Signature<'subtract', number> => (a, b) => a - b -export const multiply: Signature<'multiply', number> = (a, b) => a * b -export const absquare: Signature<'absquare', number> = a => a * a -export const reciprocal: Signature<'reciprocal', number> = a => 1 / a -export const divide: Signature<'divide', number> = (a, b) => a / b +export const add = (): Signature<'add', number> => (a, b) => a + b +const unaMinus = (a: number) => -a +export const unaryMinus = (): Signature<'unaryMinus', number> => unaMinus +export const conj = (): Signature<'conj', number> => a => a +export const subtract = (): Signature<'subtract', number> => (a, b) => a - b +export const multiply = (): Signature<'multiply', number> => (a, b) => a * b +export const absquare = (): Signature<'absquare', number> => a => a * a +export const reciprocal = (): Signature<'reciprocal', number> => a => 1 / a +export const divide = (): Signature<'divide', number> => (a, b) => a / b const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a) -export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt +export const conservativeSqrt = (): Signature<'conservativeSqrt', number> => + basicSqrt export const sqrt = (dep: configDependency @@ -23,8 +25,11 @@ export const sqrt = return a => { if (isNaN(a)) return NaN if (a >= 0) return Math.sqrt(a) - return dep.complex(0, Math.sqrt(unaryMinus(a))) + return dep.complex(0, Math.sqrt(unaMinus(a))) } } -$reflect!([add, sqrt, subtract]) -$Dreflect!([conj]) + +$reflect!([ + add, unaryMinus, conj, subtract, multiply, absquare, reciprocal, divide, + conservativeSqrt, sqrt +])