pocomath/src/complex/abs.mjs
Glen Whitney de42c22ab4 refactor: Convert resolution to two-tier system
Typed-function's sort order/matching algorithm was interfering with
  template resolution. This commit solves the difficulty by moving the
  "catchall" implementations that implement generation of new template
  instances into a separate "fallback" typed-function universe, so that
  Pocomath can control exactly when that is searched.

  Removes a couple of the matching anomalies already noted in the tests.

  Also extends return types to somewhat more functions.
2022-08-29 09:30:07 -04:00

26 lines
913 B
JavaScript

import {Returns, returnTypeOf} from '../core/Returns.mjs'
export * from './Types/Complex.mjs'
export const abs = {
'Complex<T>': ({
T,
sqrt, // Unfortunately no notation yet for the needed signature
'absquare(T)': baseabsq,
'absquare(Complex<T>)': absq
}) => {
const pm = sqrt.fromInstance
if (typeof pm === 'undefined') {
// Just checking for the dependencies, return value is irrelevant
return undefined
}
const midType = returnTypeOf(baseabsq)
const sqrtImp = pm.resolve('sqrt', midType, sqrt)
let retType = returnTypeOf(sqrtImp)
if (retType.includes('|')) {
// This is a bit of a hack, as it relies on all implementations of
// sqrt returning the "typical" return type as the first option
retType = retType.split('|',1)[0]
}
return Returns(retType, z => sqrtImp(absq(z)))
}
}