Skip to content

Instantly share code, notes, and snippets.

@peterpme
Created March 20, 2014 15:42
Show Gist options
  • Save peterpme/9666695 to your computer and use it in GitHub Desktop.
Save peterpme/9666695 to your computer and use it in GitHub Desktop.
Javascript: Calculate Math using numbers as functions. e.g.: five(plus(three())) => 8
function plus(x){
return function(y){
return y+x;
};
}
function minus(x){
return function(y){
return y-x;
};
}
function times(x){
return function(y){
return y*x;
};
}
function dividedBy(x){
return function(y){
return y/x;
};
}
function one(fn){
return fn ? fn.call(void 0, 1) : 1;
}
function two(fn){
return fn ? fn.call(void 0, 2) : 2;
}
function three(fn){
return fn ? fn.call(void 0, 3) : 3;
}
function number(num){
return function(fn){
return fn ? fn.call(void 0, num) : num;
};
}
function four(fn){
return number(4)(fn);
}
function five(fn){
return number(5)(fn);
}
function six(fn){
return number(6)(fn);
}
function seven(fn){
return number(7)(fn);
}
function eight(fn){
return number(8)(fn);
}
function nine(fn){
return number(9)(fn);
}
console.log(four(dividedBy(three())));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment