refactor: require match to merge any function (#23)
All checks were successful
/ test (push) Successful in 16s

Resolves #12.

Reviewed-on: #23
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2025-04-24 00:16:04 +00:00 committed by Glen Whitney
parent 236f46c0c0
commit 0ff00ff8cb
4 changed files with 14 additions and 8 deletions

View file

@ -1,4 +1,6 @@
import ArrayKeyedMap from 'array-keyed-map'
import {match} from './helpers.js'
import {Passthru} from './TypePatterns.js'
// Generic types are callable, so we have no choice but to extend Function
export class Type extends Function {
@ -86,7 +88,7 @@ export const whichType = typs => Returns(TypeOfTypes, item => {
throw new TypeError(errorMsg)
})
export const typeOf = math => whichType(math.types)
export const typeOf = match(Passthru, math => whichType(math.types))
// bootstrapping order matters, but order of exports in a module isn't
// simply the order that the items are listed in the module. So we make

View file

@ -103,6 +103,10 @@ export class TypeDispatcher {
// right here:
if (val instanceof Matcher) val = new Implementations(val)
if (Array.isArray(val)) val = new Implementations(val)
if (isPlainFunction(val)) {
throw new RangeError(
`function value for ${key} must be merged within a 'match' call`)
}
if (!(val instanceof Implementations)) {
val = new Implementations(match(Passthru, val))
}

View file

@ -1,7 +1,7 @@
import {ReturnsAs} from './helpers.js'
import {match} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
import {Any, matched} from '#core/TypePatterns.js'
import {Any, Passthru, matched} from '#core/TypePatterns.js'
import {boolnum} from '#number/helpers.js'
export const equal = match([Any, Any], (math, [T, U]) => {
@ -76,10 +76,10 @@ export const sign = match(Any, (math, T) => {
return ReturnsAs(comp, t => comp(t, zero))
})
export const unequal = (math, types) => {
export const unequal = match(Passthru, (math, types) => {
const eq = math.equal.resolve(types)
return ReturnsAs(eq, (...args) => !eq(...args))
}
})
export const larger = match([Any, Any], (math, [T, U]) => {
const eq = math.equal.resolve([T, U])

View file

@ -1,13 +1,13 @@
import {ReturnsAs} from './helpers.js'
import {ResolutionError} from '#core/helpers.js'
import {ResolutionError, match} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
import {Any} from "#core/TypePatterns.js"
import {Passthru} from "#core/TypePatterns.js"
export const isZero = (math, [T]) => {
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))
}
})