2022-03-25 07:49:03 +00:00
|
|
|
/* Totally minimal "typed functions" */
|
|
|
|
|
|
|
|
const addImps = (dest, imps) => {
|
|
|
|
if (imps) {
|
|
|
|
if (!Array.isArray(imps[0])) imps = [imps]
|
|
|
|
for (const imp of imps) dest.push(imp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function poortf (name, imps) {
|
|
|
|
/* This is the (function) object we will return */
|
|
|
|
function fn () {
|
|
|
|
for (const imp of fn.imps) {
|
|
|
|
if (imp[0](arguments)) return imp[1].apply(null, arguments)
|
|
|
|
}
|
|
|
|
throw new TypeError(
|
|
|
|
`TF ${fn.name}: No match for ${arguments[0]}, ${arguments[1]}, ...`)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now dress it up for use */
|
|
|
|
Object.defineProperty(fn, 'name', {value: name})
|
|
|
|
fn.imps = []
|
|
|
|
addImps(fn.imps, imps)
|
|
|
|
fn.addImps = newI => addImps(fn.imps, newI)
|
|
|
|
|
|
|
|
return fn
|
|
|
|
}
|
2022-03-25 09:46:49 +00:00
|
|
|
|
|
|
|
export function isTF(x) {
|
|
|
|
return typeof x === 'function' && 'imps' in x && Array.isArray(x.imps)
|
|
|
|
}
|