2022-12-21 05:18:42 +00:00
|
|
|
import {
|
|
|
|
joinTypes, typeOfDependency, Dependency, BBinary, ImpType, ImpReturns
|
|
|
|
} from '../core/Dispatcher.js'
|
2022-12-07 01:21:05 +00:00
|
|
|
|
|
|
|
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' && 're' in z && 'im' in z
|
|
|
|
&& dep.testT(z.re) && dep.testT(z.im),
|
2022-12-19 22:14:26 +00:00
|
|
|
infer: (dep: typeOfDependency) =>
|
2022-12-07 01:21:05 +00:00
|
|
|
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
|
|
|
|
from: {
|
2022-12-19 22:14:26 +00:00
|
|
|
T: <T>(dep: Dependency<'zero', [T]>) => (t: T) =>
|
2022-12-07 01:21:05 +00:00
|
|
|
({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)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 05:18:42 +00:00
|
|
|
export interface ComplexReturn<Params> {
|
2022-12-21 16:41:25 +00:00
|
|
|
// Sadly, I can't think of a way to make some nice abbreviation operators
|
|
|
|
// for these generic type specifications because TypeScript generics
|
|
|
|
// can't take and use generic parameters, only fully instantiated types.
|
2022-12-21 05:18:42 +00:00
|
|
|
complex: Params extends [infer U] ? Complex<U> // unary case
|
|
|
|
: Params extends BBinary<infer B> ? Complex<B> // binary case
|
|
|
|
: never
|
|
|
|
}
|
|
|
|
|
|
|
|
export const complex_unary =
|
|
|
|
<T>(dep: Dependency<'zero', [T]>): ImpType<'complex', [T]> =>
|
|
|
|
t => ({re: t, im: dep.zero(t)})
|
|
|
|
export const complex_binary = <T>(t: T, u: T): ImpReturns<'complex', [T,T]> =>
|
|
|
|
({re: t, im: u})
|