functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
218 lines (217 loc) • 6.98 kB
JavaScript
import { get, merge, fromRange, rangeMap } from "./module.f.js";
import { unsafeCmp } from "../function/compare/module.f.js";
import * as json from "../../json/module.f.js";
import { sort } from "../object/module.f.js";
import { union } from "../sorted_set/module.f.js";
import { equal, toArray } from "../list/module.f.js";
import * as operator from "../function/operator/module.f.js";
const stringify = json.stringify(sort);
const op = {
union: union(unsafeCmp),
equal: equal(operator.strictEqual),
def: []
};
export default {
example: () => {
const rmOps = rangeMap({
union: a => b => a | b,
equal: a => b => a === b,
def: 0,
});
// Create range maps
const range1 = rmOps.fromRange([0, 10])(2);
const range2 = rmOps.fromRange([5, 15])(5);
// Merge range maps
const merged = toArray(rmOps.merge(range1)(range2));
// Retrieve values from the merged range map
//
if (rmOps.get(-1)(merged) !== 0) {
throw 'error';
}
//
if (rmOps.get(0)(merged) !== 2) {
throw 'error';
}
if (rmOps.get(2)(merged) !== 2) {
throw 'error';
}
// 2 | 5 = 7
if (rmOps.get(7)(merged) !== 7) {
throw 'error';
}
//
if (rmOps.get(12)(merged) !== 5) {
throw 'error';
}
if (rmOps.get(15)(merged) !== 5) {
throw 'error';
}
//
if (rmOps.get(16)(merged) !== 0) {
throw 'error';
}
},
merge: [
() => {
const a = [[['a'], 1], [['b'], 2]];
const b = null;
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a"],1],[["b"],2]]') {
throw result;
}
},
() => {
const a = null;
const b = [[['a'], 1], [['b'], 2]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a"],1],[["b"],2]]') {
throw result;
}
},
() => {
const a = [[['a'], 1], [['b'], 2]];
const b = [[['a'], 1], [['b'], 2]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a"],1],[["b"],2]]') {
throw result;
}
},
() => {
const a = [[['a'], 1], [['c'], 3]];
const b = [[['b'], 2], [['d'], 4]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a","b"],1],[["b","c"],2],[["c","d"],3],[["d"],4]]') {
throw result;
}
},
() => {
const a = [[['a'], 1], [['d'], 4]];
const b = [[['b'], 2], [['c'], 3]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a","b"],1],[["b","d"],2],[["c","d"],3],[["d"],4]]') {
throw result;
}
},
() => {
const a = [[['a'], 1], [['b'], 2]];
const b = [[['b'], 1], [['a'], 2]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a","b"],2]]') {
throw result;
}
},
() => {
const a = [[['a'], 1], [['b'], 2], [['a'], 3]];
const b = [[['a'], 5]];
const merged = merge(op)(a)(b);
const result = stringify(toArray(merged));
if (result !== '[[["a"],1],[["a","b"],2],[["a"],5]]') {
throw result;
}
}
],
get: () => {
const sortedSetEmpty = [];
const at = get(sortedSetEmpty);
return [
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(5)(rm));
if (result !== '["a"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(10)(rm));
if (result !== '["a"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(15)(rm));
if (result !== '["b"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(20)(rm));
if (result !== '["b"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(25)(rm));
if (result !== '["c"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(30)(rm));
if (result !== '["c"]') {
throw result;
}
},
() => {
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
const result = stringify(at(35)(rm));
if (result !== '[]') {
throw result;
}
},
() => {
const rm = [];
const result = stringify(at(10)(rm));
if (result !== '[]') {
throw result;
}
}
];
},
fromRange: () => {
const def = -1;
const rm = fromRange(def)([1, 7])(42);
return [
() => {
const result = get(def)(0)(rm);
if (result !== -1) {
throw result;
}
},
() => {
const result = get(def)(1)(rm);
if (result !== 42) {
throw result;
}
},
() => {
const result = get(def)(3)(rm);
if (result !== 42) {
throw result;
}
},
() => {
const result = get(def)(7)(rm);
if (result !== 42) {
throw result;
}
},
() => {
const result = get(def)(9)(rm);
if (result !== -1) {
throw result;
}
},
];
}
};