@thi.ng/tensors
Version:
0D/1D/2D/3D/4D tensors with extensible polymorphic operations and customizable storage
16 lines (15 loc) • 519 B
JavaScript
import { unsupported } from "@thi.ng/errors/unsupported";
const top = (dispatch = 1, fallback, ...optimized) => {
const impls = [void 0].concat(optimized);
const fn = (...args) => {
const g = impls[args[dispatch].dim] || fallback;
return g ? g(...args) : unsupported(`no impl for dimension ${args[dispatch].dim}`);
};
fn.add = (dim, fn2) => impls[dim] = fn2;
fn.default = (fn2) => fallback = fn2;
fn.impl = (dim) => dim != null ? impls[dim] || fallback : fallback;
return fn;
};
export {
top
};