big part of the code base still needs to be 'ported' to the updated type constructors.
101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
import { lsType, setType } from "../structures/types.js";
|
|
import { pretty } from "./pretty.js";
|
|
|
|
// re-inventing the wheel:
|
|
export function deepEqual(a, b) {
|
|
if (a === b) return true; // <- shallow equality and primitives
|
|
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
|
|
return false;
|
|
}
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
if (a.length !== b.length) return false;
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (!deepEqual(a[i], b[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
if (a instanceof Set) {
|
|
if (!(b instanceof Set)) {
|
|
return false;
|
|
}
|
|
if (a.size !== b.size) {
|
|
return false;
|
|
}
|
|
for (const entry of a) {
|
|
if (!b.has(entry)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
const keysA = Object.keys(a);
|
|
const keysB = Object.keys(b);
|
|
if (keysA.length !== keysB.length) return false;
|
|
for (let key of keysA) {
|
|
if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 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());
|
|
|
|
const _transformType = mapping => transform => type => {
|
|
const found = mapping.get(type);
|
|
if (found) {
|
|
return found;
|
|
}
|
|
const mapped = transform(type, _transformType(mapping)(transform));
|
|
mapping.set(type, mapped);
|
|
return mapped;
|
|
}
|
|
|
|
export const transformType = _transformType(new Map());
|
|
|
|
const __memo = () => {
|
|
let memo;
|
|
return fn => memo || (memo = fn());
|
|
}
|
|
export const memo = fn => {
|
|
return __memo()(fn);
|
|
}
|
|
|
|
// let infiniteSet = mapRecursiveStructure((type, map) => {
|
|
// const ps = [];
|
|
// for (const p of type.params) {
|
|
// ps.push(map(p()));
|
|
// }
|
|
// return setType(ps[0]);
|
|
// })(infiniteList)();
|
|
// console.log(infiniteSet);
|
|
// // while (true) {
|
|
// // console.log(infiniteSet);
|
|
// // infiniteSet = infiniteSet.params[0]();
|
|
// // }
|