reorganize directory and file structure

This commit is contained in:
Joeri Exelmans 2025-05-07 13:44:49 +02:00
parent 1d826ea8d4
commit 48390b8556
99 changed files with 1155 additions and 1629 deletions

18
lib/util/defaultmap.js Normal file
View file

@ -0,0 +1,18 @@
// export class DefaultMap {
// constructor(defaultValue, ...rest) {
// this.defaultValue = defaultValue;
// this.m = new Map(rest);
// }
// getdefault(key, addToMapping = false) {
// return this.m.get(key) || (() => {
// const val = this.defaultValue(key);
// if (addToMapping)
// this.m.set(key, val);
// return val;
// })();
// }
// entries() {
// return this.m.entries();
// }
// }

49
lib/util/pretty.js Normal file
View file

@ -0,0 +1,49 @@
import { inspect } from 'node:util';
import { symbolDict, symbolFunction, symbolList, symbolProduct, symbolSum } from '../structures/type_constructors.js';
import { symbolSet } from "../structures/type_constructors.js";
import { mapRecursiveStructure } from './util.js';
import { getHumanReadableName } from '../primitives/symbol.js';
export function pretty(obj) {
return inspect(obj, { colors: true, depth: null, breakLength: 120 });
}
// Pretty print Type
export const prettyT = type => {
return mapRecursiveStructure(([type, m, seen], map) => {
if (typeof type === "symbol") {
// type variable
return type.description;
}
// if (type.params === undefined) {
// throw new Error("parameter is not a Type ... did you mean to call prettyGenT instead?")
// }
if (!m.has(type)) {
m.add(type); // next time we encounter this type, we'll only render a placeholder
const params = type.params.map(p => map([p(type), m, seen])());
// if while visiting the children, we encountered ourselves, add annotation:
const annot = seen.has(type) ? seen.get(type) : ``;
return renderType(type.symbol, annot, params);
}
if (!seen.has(type)) {
seen.set(type, `#${seen.size}`);
}
return seen.get(type);
})([type, new Set(), new Map()])();
};
const renderType = (symbol, annot, params) => {
return {
[symbolList] : `${annot}[${params[0]}]`,
[symbolSet] : `${annot}{${params[0]}}`,
[symbolFunction]: `${annot}(${params[0]} -> ${params[1]})`,
[symbolSum] : `${annot}(${params[0]} + ${params[1]})`,
[symbolProduct] : `${annot}(${params[0]} ${params[1]})`,
[symbolDict] : `${annot}(${params[0]} => ${params[1]})`,
}[symbol] || getHumanReadableName(symbol);
};
// Pretty print GenericType
export const prettyGenT = genericType => {
return `${[...genericType.typeVars].map(prettyT).sort((a, b) => a.localeCompare(b)).join(",")}: ${prettyT(genericType.type)}`;
};

16
lib/util/random.js Normal file
View file

@ -0,0 +1,16 @@
// IMPURE
export const genUUID = (len=16) => {
const arr = crypto.getRandomValues(new Uint8Array(len));
let result = "";
for (let i=0; i<len; i++) {
const unsignedByte = arr[i];
if (unsignedByte < 16) {
result += '0' + unsignedByte.toString(16);
} else {
result += unsignedByte.toString(16);
}
}
return result;
};
console.log(genUUID())

View file

@ -0,0 +1,21 @@
// Tiny wrapper around function-red-black-tree that overrides the [inspect.custom] symbol so when we print it (during debugging) we just see the (key=>value)-pairs instead of the tree structure.
import createRBTree from "functional-red-black-tree";
import { inspect } from "util";
export class RBTreeWrapper {
constructor(tree) {
this.tree = tree;
}
static new(compareFn) {
return new RBTreeWrapper(createRBTree(compareFn))
}
// pretty print to console
[inspect.custom](depth, options, inspect) {
const entries = [];
this.tree.forEach((key, val) => { entries.push(`${inspect(key)} => ${inspect(val)}`); });
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
}
}

27
lib/util/util.js Normal file
View file

@ -0,0 +1,27 @@
// zip two arrays
export function zip(a, b) {
return a.map((k, i) => [k, b[i]]);
}
export function capitalizeFirstLetter(val) {
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
}
const _mapRecursiveStructure = mapping => transform => root => {
const found = mapping.get(root);
if (found) {
// already mapped
// return existing result to prevent endless recursion
return found;
}
// note the indirection (wrapped in lamda), this allows the user to recursively map the children (which may refer to the root) without yet having finished mapping the root.
let memo;
const result = () => {
// memoization is necessary for correctness
return memo || (memo = transform(root, _mapRecursiveStructure(mapping)(transform)));
};
mapping.set(root, result);
return result;
};
export const mapRecursiveStructure = _mapRecursiveStructure(new Map());