almost there

This commit is contained in:
2018-06-01 13:56:08 +02:00
parent 2845a5e308
commit 33e378fcd1
2 changed files with 96 additions and 29 deletions

View File

@@ -1,4 +1,53 @@
module.exports = {
to_int: (f) => f(n => n+1)(0),
to_boolean: (f) => f(true)(false),
const nothing = require('./nothing.js');
const charset = '0123456789BFiuz';
const from_int = (n) => {
let f = nothing.ZERO;
for (let _ = 0; _ < n; _++) f = nothing.INC(f);
return f;
};
const to_int = (f) => nothing.TIMES(f)(i => i+1)(0);
const from_bool = (b) => b ? nothing.TRUE : nothing.FALSE;
const to_bool = (f) => f(true)(false);
const to_array = (l) => {
let a = [];
while (!to_bool(nothing.IS_EMPTY(l))) {
a.push(nothing.FIRST(l));
l = nothing.REST(l);
}
return a;
};
const from_array = (a) => {
let l = nothing.EMPTY;
while (a.length) l = nothing.UNSHIFT(l)(a.pop());
return l;
};
const to_char = (c) => charset[to_int(c)];
const from_char = (c) => from_int(charset.indexOf(c));
const to_string = (s) => to_array(s).map(to_char).join('');
const from_string = (s) => from_array(s.split('').map(from_char));
const representation_of = (v) => {
if (typeof v == "number") return from_int(v);
if (typeof v == "boolean") return from_bool(v);
if (typeof v == "string") return from_string(v);
if (Array.isArray(v)) return from_array(v.map(representation_of));
throw Error(`Oops, I dont know how represent this: ${v}`);
};
const zip = (a, b) => a.map((e, i) => [e, b[i]]);
const check_represent = function(r, v) {
if (typeof v == "number") return to_int(r) == v;
if (typeof v == "boolean") return to_bool(r) == v;
if (typeof v == "string") { console.log("checking ", to_array(r).map(to_int), " against ", v); return to_string(r) == v; }
if (Array.isArray(v)) {
let a = to_array(r);
return a.length == v.length && zip(a, v).every(([r, v]) => check_represent(r,v));
}
throw Error(`Oops, I dont know how check this: ${v}`);
};
module.exports = {
to_string, to_array, representation_of, check_represent, to_int, to_char
};