happybot/ok/appendoK.js

57 lines
2.2 KiB
JavaScript

// stolen from iKe
var katan2 = function(x, y) { return k(0, Math.atan2(n(x).v, n(y).v)); }
var kpow = function(x, y) { return k(0, Math.pow (n(x).v, n(y).v)); }
verbs["atan2"] = [null, null, ad(katan2), ad(katan2), ad(katan2), ad(katan2), null, null];
verbs["pow" ] = [null, null, ad(kpow) , ad(kpow) , ad(kpow) , ad(kpow) , null, null];
// stolen from convert.js
function trampoline(env, name, args, body) {
// construct a function-wrapper trampoline for a pseudonative.
// this permits dyadic/triadic extension functions to be properly curryable.
var arguse = []; for(var z=0; z<args.length; z++) { arguse.push({"t":7, "v":args[z]}); }
env.put(ks(name), true, {t: 5, args:args, v: [ {"t":8,"v":name,"curry":arguse} ]});
}
infix["pow"] = 0;
natives["abs"] = 0;
natives["tan"] = 0;
natives["acos"] = 0;
natives["asin"] = 0;
natives["atan"] = 0;
natives["sinh"] = 0;
natives["cosh"] = 0;
natives["tanh"] = 0;
// some custom shit
//const { execSync } = require('child_process');
//const convert = require('./convert');
//natives["randomwords"] = 0;
function extendedEnv() {
var env = baseEnv();
// nmonad("randomwords",function(x) { return convert.tok(execSync('shuf -n ' + n(x).v + ' /usr/share/web/words.txt', {encoding: 'utf-8'}).split('\n').slice(0,-1)) });
nmonad("abs", function(x) { return k(0, Math.abs (n(x).v)) });
nmonad("tan", function(x) { return k(0, Math.tan (n(x).v)) });
nmonad("acos", function(x) { return k(0, Math.acos(n(x).v)) });
nmonad("asin", function(x) { return k(0, Math.asin(n(x).v)) });
nmonad("atan", function(x) { return k(0, Math.atan(n(x).v)) });
nmonad("sinh", function(x) { x=n(x).v; return k(0, .5*Math.exp(x)-Math.exp(-x)); });
nmonad("cosh", function(x) { x=n(x).v; return k(0, .5*Math.exp(x)+Math.exp(-x)); });
nmonad("tanh", function(x) {
x=n(x).v; return k(0, (Math.exp(x)-Math.exp(-x))/(Math.exp(x)+Math.exp(-x)));
});
trampoline(env, "atan2" , ["x", "y"], katan2);
trampoline(env, "pow" , ["x", "y"], kpow);
return env;
}
this.extendedEnv = extendedEnv;
var unpackTwoIntImpl = parse("{d:x;{$[~*x;1_x;x]}/|d!{_x%d}\\y}")[0];
function unpackTwoInt(x, y) {
x = n(x); y = n(y);
x.v = 0|x.v; y.v = 0|y.v;
return call(unpackTwoIntImpl, k(3, [x,y]));
}
verbs["\\"][2] = unpackTwoInt;