import { unit } from "../primitives/unit.js"; import { capitalizeFirstLetter } from "../util/util.js"; export const makeConstructor = fieldNames => { const internal = (fieldNames, ret) => { if (fieldNames.length === 0) { const result = ret(unit); return result; } return nextParam => { const [fieldName, ...rest] = fieldNames; const wrappedName = 'ctor_' + fieldName; const newRet = { [wrappedName]: inner => ({[fieldName]: nextParam, ...ret(inner)}), }[wrappedName]; return internal(rest, newRet); } }; const id = x => x; return internal(fieldNames, id); }; export const makeGetters = fieldNames => { return fieldNames.map(fieldName => { const getterName = `get${capitalizeFirstLetter(fieldName)}`; return { [getterName]: obj => obj[fieldName], }[getterName]; }) }; export const makeStructCompareFn = compares => { return a => b => { for (const {l: fieldName, r: getCompareFn} of compares) { const cmp = getCompareFn()(a[fieldName])(b[fieldName]); if (cmp !== 0) return cmp; } return 0; } }