66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import {
|
|
joinTypes, typeOfDependency, Dependency,
|
|
} from '../core/Dispatcher.js'
|
|
import type {
|
|
ZeroType, OneType, NaNType, Dependencies, OpType, OpReturns
|
|
} from '../interfaces/type.js'
|
|
|
|
export type Complex<T> = { re: T; im: T; }
|
|
|
|
export const Complex_type = {
|
|
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
|
|
(z: unknown): z is Complex<T> =>
|
|
typeof z === 'object' && z != null && 're' in z && 'im' in z
|
|
&& dep.testT(z['re']) && dep.testT(z['im']),
|
|
infer: (dep: typeOfDependency) =>
|
|
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
|
|
from: {
|
|
T: <T>(dep: Dependency<'zero', [T]>) => (t: T) =>
|
|
({ re: t, im: dep.zero(t) }),
|
|
Complex: <U, T>(dep: { convert: (from: U) => T }) =>
|
|
(z: Complex<U>) => ({ re: dep.convert(z.re), im: dep.convert(z.im) })
|
|
}
|
|
}
|
|
|
|
declare module "../interfaces/type" {
|
|
interface AssociatedTypes<T> {
|
|
Complex: T extends Complex<infer R> ? {
|
|
type: Complex<R>
|
|
zero: Complex<ZeroType<R>>
|
|
one: Complex<OneType<R> | ZeroType<R>>
|
|
nan: Complex<NaNType<R>>
|
|
real: RealType<R>
|
|
} : never
|
|
}
|
|
|
|
interface Operations<T> {
|
|
complex: {params: [T] | [T,T], returns: Complex<T>}
|
|
}
|
|
}
|
|
|
|
export const complex =
|
|
<T>(dep: Dependencies<'zero', T>): OpType<'complex', T | ZeroType<T>> =>
|
|
(a, b) => ({re: a, im: b || dep.zero(a)})
|
|
|
|
export const zero =
|
|
<T>(dep: Dependencies<'zero', T>
|
|
& Dependencies<'complex', OpReturns<'zero', T>>):
|
|
OpType<'zero', Complex<T>> =>
|
|
z => dep.complex(dep.zero(z.re), dep.zero(z.im))
|
|
|
|
export const one =
|
|
<T>(dep: Dependencies<'one' | 'zero', T>
|
|
& Dependencies<'complex', OpReturns<'one' | 'zero', T>>):
|
|
OpType<'one', Complex<T>> =>
|
|
z => dep.complex(dep.one(z.re), dep.zero(z.im))
|
|
|
|
export const nan =
|
|
<T>(dep: Dependencies<'nan', T>
|
|
& Dependencies<'complex', OpReturns<'nan', T>>):
|
|
OpType<'nan', Complex<T>> =>
|
|
z => dep.complex(dep.nan(z.re), dep.nan(z.im))
|
|
|
|
export const re =
|
|
<T>(dep: Dependencies<'re', T>): OpType<'re', Complex<T>> =>
|
|
z => dep.re(z.re)
|