function identity_function(param) {
return function() {
return param;
}
}
function addf(x) {
return function(y) {
return x + y;
}
}
function applyf(fn) {
return function(x) {
return function(y) {
return fn(x,y);
}
}
}
function curry(fn, first) {
return function(second) {
return fn(first, second);
}
}
function inc(x) {
return addf(x)(1);
}
function methodize(fn) {
return function(x) {
return fn(this, x);
}
}
function demethodize(fn) {
return function(x,y) {
return fn.call(x, y);
}
}
function twice(fn) {
return function(x) {
return fn(x,x);
}
}
function composeu(first, second) {
return function(x) {
return second(first(x));
}
}
function composeb(first, second) {
return function(x, y, z) {
return second(first(x, y), z);
}
}
function once(fn) {
this.ran = false;
const that = this;
return function(x,y) {
if (!that.ran) {
that.ran = true;
return fn(x,y);
} else {
throw new Error("Function already ran!");
}
}
}
function counterf(value) {
const counter = { value };
counter.inc = function() {
counter.value ++;
return counter.value;
}
counter.dec = function() {
counter.value --;
return counter.value;
}
return counter;
}
function revocable(fn) {
const state = { fn };
state.invoke = function(value) {
if (state.fn) {
return fn(value);
} else {
throw new Error("No function available!");
}
}
state.revoke = function() {
state.fn = null;
}
return state;
}
function vector() {
this.vector = [];
const object = {};
const that = this;
object.append = function(value) {
that.vector.push(value);
}
object.store = function(index, value) {
that.vector.splice(index, 0, value);
that.vector.join();
}
object.get = function(index) {
return that.vector[index];
}
return object;
}
function pubsub() {
this.subs = [];
const that = this;
return {
subscribe(fn) {
that.subs.push(fn);
},
publish(value) {
that.subs.forEach(fn => {
fn(value);
});
}
}
}
function gensymf(symbol) {
this.symbol = symbol;
this.counter = 0;
return () => {
const output = `${this.symbol}${this.counter}`;
this.counter++;
return output;
}
}
function fibonacci(first, second) {
this.first = first;
this.second = second;
this.firstReturned = false;
this.secondReturned = false;
return () => {
if (!this.firstReturned) {
this.firstReturned = true;
return this.first;
} else if (!this.secondReturned) {
this.secondReturned = true;
return this.second;
} else {
const result = this.first + this.second;
this.first = this.second;
this.second = result;
return result;
}
}
}
function addg(value) {
function fn(next) {
if (!next) return value;
value += next;
return fn;
}
return fn;
}
function applyg(fn) {
let store = 0;
return function (first) {
store = first;
function cb(second) {
if (second) {
store = fn(store, second);
return cb;
} else {
return store;
}
}
return cb;
}
}
function m(value, source) {
return {
value,
source: source || String(value)
};
}
function addm(first, second) {
return {
value: first.value + second.value,
source: `(${first.value}+${second.value})`
};
}
function binarymf(fn, sign) {
return (first, second) => {
return {
value: fn(first.value, second.value),
source: `(${first.value}${sign}${second.value})`
}
}
}
function binarymf2(fn, sign) {
return (first, second) => {
let firstRaw, secondRaw;
if (typeof first === 'object' && first.hasOwnProperty('value')) {
firstRaw = first.value;
} else {
firstRaw = first;
}
if (typeof second === 'object' && second.hasOwnProperty('value')) {
secondRaw = second.value;
} else {
secondRaw = second;
}
return {
value: fn(firstRaw, secondRaw),
source: `(${firstRaw}${sign}${secondRaw})`
}
}
}
function unarymf(fn, sign) {
return (value) => {
return {
value: fn(value),
source: `(${sign} ${value})`
}
}
}
function hyp(a, b) {
return Math.sqrt(a*a + b*b);
}
function exp(array) {
const fn = array[0];
const first = array[1];
const second = array[2];
let firstRaw, secondRaw;
if (typeof first === 'number') {
firstRaw = first;
} else if (typeof first === 'object' && first.length) {
firstRaw = exp(first);
}
if (second) {
if (typeof second === 'number') {
secondRaw = second;
} else if (typeof second === 'object' && second.length) {
secondRaw = exp(second);
}
}
if (secondRaw) {
return fn(firstRaw, secondRaw);
} else {
return fn(firstRaw);
}
}
var variable;
function store(value) {
variable = value;
}
function quatre(bin, first, second, cb) {
cb(bin(first, second));
}
function unaryc(fn) {
return function(value, cb) {
cb(fn(value));
}
}
function binaryc(binary) {
return function (first, second, cb) {
cb(binary(first, second));
}
}