feat: Complete relational functions for vectors
Toward its goal, this commit also: * Adds a new section of logical functions, and defines `not`, `and`, `or` for all current types. * Extends `OneOf` choice/union type to allow argument types that are themselves `OneOf` types. * Adds a readable .toString() method for TypePatterns. * Defines negate (as a no-op) and isnan (as always true) for the Undefined type * Extends comparisons to the Undefined type (to handle comparing vectors of different lengths)
This commit is contained in:
parent
c3c2bbbf78
commit
edfba089e3
19 changed files with 238 additions and 19 deletions
|
@ -1,6 +1,6 @@
|
|||
import {Vector} from './Vector.js'
|
||||
import {NotAType, Returns} from '#core/Type.js'
|
||||
import {match} from '#core/TypePatterns.js'
|
||||
import {NotAType, Returns, Undefined} from '#core/Type.js'
|
||||
import {Any, match} from '#core/TypePatterns.js'
|
||||
|
||||
export const promoteUnary = name => match(Vector, (math, V, strategy) => {
|
||||
if (V.Component === NotAType) {
|
||||
|
@ -12,4 +12,68 @@ export const promoteUnary = name => match(Vector, (math, V, strategy) => {
|
|||
return Returns(Vector(compOp.returns), v => v.map(elt => compOp(elt)))
|
||||
})
|
||||
|
||||
|
||||
export const promoteBinary = name => [
|
||||
match([Vector, Any], (math, [V, E], strategy) => {
|
||||
const VComp = V.Component
|
||||
if (VComp === NotAType) {
|
||||
return Returns(V, (v, e) => v.map(
|
||||
f => math.resolve(name, [math.typeOf(f), E], strategy)(f, e)))
|
||||
}
|
||||
const compOp = math.resolve(name, [VComp, E], strategy)
|
||||
return Returns(
|
||||
Vector(compOp.returns), (v, e) => v.map(f => compOp(f, e)))
|
||||
}),
|
||||
match([Any, Vector], (math, [E, V], strategy) => {
|
||||
const VComp = V.Component
|
||||
if (VComp === NotAType) {
|
||||
return Returns(V, (e, v) => v.map(
|
||||
f => math.resolve(name, [E, math.typeOf(f)], strategy)(e, f)))
|
||||
}
|
||||
const compOp = math.resolve(name, [E, VComp], strategy)
|
||||
return Returns(
|
||||
Vector(compOp.returns, (e, v) => v.map(f => compOp(e, f))))
|
||||
}),
|
||||
match([Vector, Vector], (math, [V, W], strategy) => {
|
||||
const VComp = V.Component
|
||||
const WComp = W.Component
|
||||
let compOp
|
||||
let opNoV
|
||||
let opNoW
|
||||
let ReturnType
|
||||
if (VComp === NotAType || WComp === NotAType) {
|
||||
const typ = math.typeOf
|
||||
compOp = (v, w) => {
|
||||
return math.resolve(name, [typ(v), typ(w)], strategy)(v, w)
|
||||
}
|
||||
opNoV = compOp
|
||||
opNoW = compOp
|
||||
ReturnType = Vector(NotAType)
|
||||
} else {
|
||||
compOp = math.resolve(name, [VComp, WComp], strategy)
|
||||
opNoV = math.resolve(name, [Undefined, WComp], strategy)
|
||||
opNoW = math.resolve(name, [VComp, Undefined], strategy)
|
||||
ReturnType = Vector(compOp.returns)
|
||||
}
|
||||
return Returns(
|
||||
ReturnType,
|
||||
(v, w) => {
|
||||
const vInc = Number(v.length > 1)
|
||||
const wInc = Number(w.length >= v.length || w.length > 1)
|
||||
const retval = []
|
||||
let vIx = 0
|
||||
let wIx = 0
|
||||
while ((vInc && vIx < v.length)
|
||||
|| (wInc && wIx < w.length)
|
||||
) {
|
||||
if (vIx >= v.length) {
|
||||
retval.push(opNoV(undefined, w[wIx]))
|
||||
} else if (wIx >= w.length) {
|
||||
retval.push(opNoW(v[vIx], undefined))
|
||||
} else retval.push(compOp(v[vIx], w[wIx]))
|
||||
vIx += vInc
|
||||
wIx += wInc
|
||||
}
|
||||
return retval
|
||||
})
|
||||
})
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue