Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculating With Functions. eg. seven(times(five()));
function makeNum(num, func) {
if (func === undefined) {
return num;
} else {
return func(num);
}
}
function zero(func) {
return makeNum(0,func);
}
function one(func) {
return makeNum(1,func);
}
function two(func) {
return makeNum(2,func);
}
function three(func) {
return makeNum(3,func);
}
function four(func) {
return makeNum(4,func);
}
function five(func) {
return makeNum(5,func);
}
function six(func) {
return makeNum(6,func);
}
function seven(func) {
return makeNum(7,func);
}
function eight(func) {
return makeNum(8,func);
}
function nine(func) {
return makeNum(9,func);
}
function plus(right) {
return function(left) { return left + right; };
}
function minus(right) {
return function(left) {
return left - right;
};
}
function times(right) {
return function(left) { return left * right; };
}
function dividedBy(right) {
return function(left) { return left / right; };
}
eight(minus(three())); // return 5
six(dividedBy(two())); // return 3
two(plus(five())); // return 7
five(plus(two())); // return 7
seven(times(five())); // return 35
console.log(four(plus(nine()))); // must return 13
@veronaten

This comment has been minimized.

Copy link

@veronaten veronaten commented Jun 30, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment