feat: extend all utils to vectors elementwise & fix parameter match grouping

This commit is contained in:
Glen Whitney 2025-04-28 19:32:12 -07:00
parent f002310581
commit 3b882f3d53
5 changed files with 33 additions and 4 deletions

View file

@ -51,7 +51,8 @@ class SequencePattern extends TypePattern {
const [newPos, newMatch] = pat.match(typeSequence, options)
if (newPos < 0) return [-1, Undefined]
options.position = newPos
matches.push(newMatch)
if (Array.isArray(newMatch)) matches.push(...newMatch)
else matches.push(newMatch)
}
return [options.position, matches]
}
@ -125,7 +126,7 @@ class OptionalPattern extends TypePattern {
options.position = newPos
matches.push(newMatch)
}
return [options.position, matches]
return [options.position, [matches]]
}
sampleTypes() {return []}
equal(other) {
@ -148,7 +149,7 @@ class MultiPattern extends TypePattern {
const matches = []
while (true) {
const [newPos, newMatch] = this.pattern.match(typeSequence, options)
if (newPos < 0) return [options.position, matches]
if (newPos < 0) return [options.position, [matches]]
options.position = newPos
matches.push(newMatch)
}

View file

@ -53,6 +53,10 @@ describe('Type patterns', () => {
midOpt.match([Undefined, TypeOfTypes, TypeOfTypes]),
[-1, Undefined])
assert.deepStrictEqual(midOpt.sampleTypes(), [Undefined, Undefined])
const justMulti = pattern(Multiple(Undefined))
assert.deepStrictEqual(
justMulti.match([Undefined, Undefined]),
[2, [[Undefined, Undefined]]])
const midMulti = pattern([Undefined, Multiple(TypeOfTypes), Undefined])
assert.deepStrictEqual(
midMulti.match([Undefined, Undefined]),

View file

@ -18,7 +18,7 @@ export const isZero = match(Passthru, (math, [T]) => {
const eq = math.equal.resolve([T, T])
return ReturnsAs(eq, x => eq(z, x))
})
export const nonImaginary = match(Passthru, boolnum(() => true))
export const nonImaginary = match(Any, boolnum(() => true))
export const re = match(Any, (_math, T) => Returns(T, t => t))
export const im = match(Any, (math, T) => {
const z = math.zero(T)

View file

@ -18,6 +18,23 @@ describe('Vector utility functions', () => {
assert(clone3[0] !== subj3[0])
assert(clone3[1] !== subj3[1])
})
it('performs utilities elementwise', () => {
const cplx = math.complex
const sink = math.vector(
NaN, -Infinity, 7,
cplx(3, 4), cplx(3.5, -2.5), cplx(2.2), cplx(cplx(3, 4))
)
const t = true, f = false
assert.deepStrictEqual(math.isnan(sink), [t, f, f, f, f, f, f])
assert.deepStrictEqual(math.isfinite(sink), [f, f, t, t, t, t, t])
assert.deepStrictEqual(math.isInteger(sink), [f, f, t, t, f, f, t])
assert.deepStrictEqual(math.isReal(sink), [t, t, t, f, f, t, f])
assert.deepStrictEqual(math.nonImaginary(sink), [t, t, t, f, f, t, t])
assert.deepStrictEqual(math.re(sink), [NaN, -Infinity, 7, 3, 3.5, 2.2, 3])
assert.deepStrictEqual(
math.im(sink),
[0, 0, 0, cplx(0, 4), cplx(0, -2.5), cplx(0, 0), cplx(cplx(0, 4))])
})
})

View file

@ -1,3 +1,10 @@
import {promoteUnary} from './helpers.js'
export const clone = promoteUnary('clone')
export const isnan = promoteUnary('isnan')
export const isfinite = promoteUnary('isfinite')
export const isInteger = promoteUnary('isInteger')
export const isReal = promoteUnary('isReal')
export const nonImaginary = promoteUnary('nonImaginary')
export const re = promoteUnary('re')
export const im = promoteUnary('im')