nanomath/src/generic/utils.js
Glen Whitney aad62df8ac
All checks were successful
/ test (push) Successful in 17s
feat: methods on Complex (#24)
Adds all of the pocomath functions on Complex that do not depend on any unimplemented types or config properties, except quotient and roundquotient, where the design is murky. To get this working, adds some additional features:
  * Allows conversions to generic types, with the matched type
    determined from the return value of the built convertor
  * Adds predicate-based type patterns
  * Adds conversion from any non-complex type T to Complex(T)
  * Adds check for recursive loops in resolve (a key/signature depending on itself)

Reviewed-on: #24
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-04-25 14:17:34 +00:00

15 lines
597 B
JavaScript

import {ReturnsAs} from './helpers.js'
import {ResolutionError} from '#core/helpers.js'
import {Passthru, match} from '#core/TypePatterns.js'
import {boolnum} from '#number/helpers.js'
// Most types are real. Have to make sure to redefine on all non-real types
export const isReal = match(Passthru, boolnum(() => true))
export const isZero = match(Passthru, (math, [T]) => {
if (!T) { // called with no arguments
throw new ResolutionError('isZero() requires one argument')
}
const z = math.zero(T)
const eq = math.equal.resolve([T, T])
return ReturnsAs(eq, x => eq(z, x))
})