refactor: Settle on making all implementations (possibly vacuous) factories

This commit is contained in:
Glen Whitney 2023-10-16 18:48:26 -07:00
parent ebe7cf831e
commit d8199341e0
4 changed files with 30 additions and 35 deletions

View File

@ -97,4 +97,8 @@ export const sqrt =
const denom = dep.conservativeSqrt(denomsq) const denom = dep.conservativeSqrt(denomsq)
return dep.divideReal(num, denom) return dep.divideReal(num, denom)
} }
$reflect!([sqrt])
$reflect!([
add, addReal, unaryMinus, conj, subtract, multiply, absquare, divideReal,
reciprocal, divide, sqrt
])

View File

@ -1,15 +1,7 @@
import {inspect} from 'node:util' import {inspect} from 'node:util'
import {Dispatcher} from './core/Dispatcher.js' import {Dispatcher} from './core/Dispatcher.js'
import {Signatures, Deps} from './interfaces/type.js'
import {$$typeToString} from 'ts-macros'
import * as Specifications from './all.js' import * as Specifications from './all.js'
class Placeholder {}
const allSignatures =
$$typeToString!<Deps<Signatures<Placeholder>>>(true, false, true)
console.log('Found signatures', allSignatures)
export default new Dispatcher(Specifications) export default new Dispatcher(Specifications)
import {Complex} from './Complex/type.js' import {Complex} from './Complex/type.js'

View File

@ -74,13 +74,12 @@ export interface Signatures<T> {
type SignatureKey<T> = keyof Signatures<T> type SignatureKey<T> = keyof Signatures<T>
export type Signature<Name extends SignatureKey<T>, T> = export type Signature<Name extends SignatureKey<T>, T> = Signatures<T>[Name]
Signatures<T>[Name] export type Returns<Name extends SignatureKey<T>, T> =
export type DSignature<Name extends SignatureKey<T>, T> = ReturnType<Signatures<T>[Name]>
Signatures<T>[Name] & {reflectedType?: string, actualType?: Signatures<T>[Name]} type Deps<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
export type Returns<Name extends SignatureKey<T>, T> = ReturnType<Signatures<T>[Name]> export type Dependencies<Name extends SignatureKey<T>, T> =
export type Deps<T> = T extends unknown ? { [K in keyof T]: T[K] } : never; Deps<{[K in Name]: Signature<K, T>}>
export type Dependencies<Name extends SignatureKey<T>, T> = Deps<{[K in Name]: Signature<K, T>}>
export type AliasOf<Name extends string, T> = T & {aliasOf?: Name} export type AliasOf<Name extends string, T> = T & {aliasOf?: Name}
@ -89,8 +88,3 @@ export function $reflect<ImplTuple>(tup: ImplTuple) {
+[[tup], <T>(elt: T) => +[[tup], <T>(elt: T) =>
elt.reflectedType = $$typeToString!<T>(true, false, true)] elt.reflectedType = $$typeToString!<T>(true, false, true)]
} }
export function $Dreflect<ImplTuple>(tup: ImplTuple) {
+[[tup], <T>(elt: T) =>
elt.reflectedType = $$typeToString!<T['actualType']>(true, false, true)]
}
export type RTT = {reflectedType?: string}

View File

@ -1,18 +1,20 @@
import type {configDependency} from '../core/Config.js' import type {configDependency} from '../core/Config.js'
import type {Dependencies, Signature, RTT, DSignature} from '../interfaces/type.js' import type {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect, $Dreflect} from '../interfaces/type.js' import {$reflect} from '../interfaces/type.js'
export const add: Signature<'add', number> & RTT = (a, b) => a + b export const add = (): Signature<'add', number> => (a, b) => a + b
export const unaryMinus: Signature<'unaryMinus', number> = a => -a const unaMinus = (a: number) => -a
export const conj: DSignature<'conj', number> = a => 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 subtract = (): Signature<'subtract', number> => (a, b) => a - b
export const multiply: Signature<'multiply', 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 absquare = (): Signature<'absquare', number> => a => a * a
export const reciprocal: Signature<'reciprocal', number> = a => 1 / a export const reciprocal = (): Signature<'reciprocal', number> => a => 1 / a
export const divide: Signature<'divide', number> = (a, b) => a / b export const divide = (): Signature<'divide', number> => (a, b) => a / b
const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a) 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 = export const sqrt =
(dep: configDependency (dep: configDependency
@ -23,8 +25,11 @@ export const sqrt =
return a => { return a => {
if (isNaN(a)) return NaN if (isNaN(a)) return NaN
if (a >= 0) return Math.sqrt(a) 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
])