refactor: change onType to match and take only one pattern and result (#22)
All checks were successful
/ test (push) Successful in 17s

Pursuant to #12. Besides changing the name of onType to match, and only allowing one pattern and result in `match()`,
this PR also arranges that in place of an onType with lots of alternating PATTERN, VALUE, PATTERN, VALUE arguments, one now exports an _array_ of `match(PATTERN, VALUE)` items.

Doesn't quite fully resolve #12, because there is still the question of whether `match(...)` can be left out for a behavior that literally matches anything (current behavior), or whether `match(Passthru, behavior)` should be required for such cases.

Reviewed-on: #22
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-22 05:01:21 +00:00 committed by Glen Whitney
parent 491e207fad
commit 236f46c0c0
22 changed files with 147 additions and 135 deletions

View file

@ -1,9 +1,9 @@
import {Type} from '#core/Type.js'
import {onType} from '#core/helpers.js'
import {match} from '#core/helpers.js'
import {BooleanT} from '#boolean/BooleanT.js'
export const NumberT = new Type(n => typeof n === 'number', {
from: onType(BooleanT, math => math.number.resolve([BooleanT])),
from: match(BooleanT, math => math.number.resolve([BooleanT])),
one: 1,
zero: 0,
nan: NaN

View file

@ -14,9 +14,9 @@ describe('NumberT Type', () => {
it('can convert from BooleanT to NumberT', () => {
const convertImps = NumberT.from
let cnvBtoN
for (const [pattern, convFactory] of convertImps.patterns) {
for (const {pattern, does} of convertImps) {
if (pattern.match([BooleanT])[0] === 1) {
cnvBtoN = convFactory(math)
cnvBtoN = does(math)
break
}
}

View file

@ -1,9 +1,9 @@
import {NumberT} from './NumberT.js'
import {onType} from '#core/helpers.js'
import {match} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
export const plain = f => onType(
export const plain = f => match(
Array(f.length).fill(NumberT), Returns(NumberT, f))
// Takes a behavior returning boolean, and returns a factory

View file

@ -1,4 +1,4 @@
import {onType} from '#core/helpers.js'
import {match} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
import {Optional} from '#core/TypePatterns.js'
import {boolnum} from './helpers.js'
@ -11,7 +11,7 @@ import {NumberT} from './NumberT.js'
// Notice a feature of TypedDispatcher: if you specify one tolerance, you must
// specify both.
export const indistinguishable = onType(
export const indistinguishable = match(
[NumberT, NumberT, Optional([NumberT, NumberT])],
boolnum((a, b, [tolerances = [0, 0]]) => {
const [relTol, absTol] = tolerances
@ -34,4 +34,4 @@ export const indistinguishable = onType(
// Returns truthy if a (interpreted as completely precise) represents a
// greater value than b (interpreted as completely precise). Note that even if
// so, a and b might be indistinguishable() to some tolerances.
export const exceeds = onType([NumberT, NumberT], boolnum((a, b) => a > b))
export const exceeds = match([NumberT, NumberT], boolnum((a, b) => a > b))

View file

@ -1,13 +1,14 @@
import {plain} from './helpers.js'
import {BooleanT} from '#boolean/BooleanT.js'
import {match} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
import {NumberT} from '#number/NumberT.js'
const num = f => Returns(NumberT, f)
export const number = plain(a => a)
number.also(
export const number = [
plain(a => a),
// conversions from Boolean should be consistent with one and zero:
BooleanT, num(p => p ? NumberT.one : NumberT.zero),
[], num(() => 0)
)
match(BooleanT, num(p => p ? NumberT.one : NumberT.zero)),
match([], num(() => 0))
]

View file

@ -2,7 +2,7 @@ import {plain, boolnum} from './helpers.js'
import {NumberT} from './NumberT.js'
import {Returns} from '#core/Type.js'
import {onType} from '#core/helpers.js'
import {match} from '#core/helpers.js'
export const clone = plain(a => a)
export const isnan = onType(NumberT, boolnum(isNaN))
export const isnan = match(NumberT, boolnum(isNaN))