refactor: change onType to match and take only one pattern and result (#22)
All checks were successful
/ test (push) Successful in 17s
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:
parent
491e207fad
commit
236f46c0c0
22 changed files with 147 additions and 135 deletions
|
@ -1,10 +1,10 @@
|
|||
import {ReturnsAs} from './helpers.js'
|
||||
import {onType} from '#core/helpers.js'
|
||||
import {match} from '#core/helpers.js'
|
||||
import {Returns} from '#core/Type.js'
|
||||
import {Any, matched} from '#core/TypePatterns.js'
|
||||
import {boolnum} from '#number/helpers.js'
|
||||
|
||||
export const equal = onType([Any, Any], (math, [T, U]) => {
|
||||
export const equal = match([Any, Any], (math, [T, U]) => {
|
||||
// Finding the correct signature of `indistinguishable` to use for
|
||||
// testing (approximate) equality is tricky, because T or U might
|
||||
// need to be converted for the sake of comparison, and some types
|
||||
|
@ -38,7 +38,7 @@ export const equal = onType([Any, Any], (math, [T, U]) => {
|
|||
// now that we have `equal` and `exceeds`, pretty much everything else should
|
||||
// be easy:
|
||||
|
||||
export const compare = onType([Any, Any], (math, [T, U]) => {
|
||||
export const compare = match([Any, Any], (math, [T, U]) => {
|
||||
const eq = math.equal.resolve([T, U])
|
||||
const gt = math.exceeds.resolve([T, U])
|
||||
const zero = math.zero(T) // asymmetry here is unfortunate, but we have
|
||||
|
@ -70,7 +70,7 @@ export const compare = onType([Any, Any], (math, [T, U]) => {
|
|||
})
|
||||
})
|
||||
|
||||
export const sign = onType(Any, (math, T) => {
|
||||
export const sign = match(Any, (math, T) => {
|
||||
const zero = math.zero(T)
|
||||
const comp = math.compare.resolve([T, T])
|
||||
return ReturnsAs(comp, t => comp(t, zero))
|
||||
|
@ -81,25 +81,25 @@ export const unequal = (math, types) => {
|
|||
return ReturnsAs(eq, (...args) => !eq(...args))
|
||||
}
|
||||
|
||||
export const larger = onType([Any, Any], (math, [T, U]) => {
|
||||
export const larger = match([Any, Any], (math, [T, U]) => {
|
||||
const eq = math.equal.resolve([T, U])
|
||||
const bigger = math.exceeds.resolve([T, U])
|
||||
return boolnum((t, u) => !eq(t, u) && bigger(t, u))(math)
|
||||
})
|
||||
|
||||
export const largerEq = onType([Any, Any], (math, [T, U]) => {
|
||||
export const largerEq = match([Any, Any], (math, [T, U]) => {
|
||||
const eq = math.equal.resolve([T, U])
|
||||
const bigger = math.exceeds.resolve([T, U])
|
||||
return ReturnsAs(bigger, (t, u) => eq(t, u) || bigger(t, u))
|
||||
})
|
||||
|
||||
export const smaller = onType([Any, Any], (math, [T, U]) => {
|
||||
export const smaller = match([Any, Any], (math, [T, U]) => {
|
||||
const eq = math.equal.resolve([T, U])
|
||||
const bigger = math.exceeds.resolve([U, T])
|
||||
return boolnum((t, u) => !eq(t, u) && bigger(u, t))(math)
|
||||
})
|
||||
|
||||
export const smallerEq = onType([Any, Any], (math, [T, U]) => {
|
||||
export const smallerEq = match([Any, Any], (math, [T, U]) => {
|
||||
const eq = math.equal.resolve([T, U])
|
||||
const bigger = math.exceeds.resolve([U, T])
|
||||
return ReturnsAs(bigger, (t, u) => eq(t, u) || bigger(u, t))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue