Skip to content

Instantly share code, notes, and snippets.

@peterpme
Created March 5, 2014 21:30
Show Gist options
  • Save peterpme/9377055 to your computer and use it in GitHub Desktop.
Save peterpme/9377055 to your computer and use it in GitHub Desktop.
Javascript: Partial Applications: A combinator that pulls in a function regardless of one or two variables and outputs the answer.
function power(x,y){
var tmp=x;
if (y<1){
return 1;
}
else{
for(var i=0;i<y-1;i++){
tmp=tmp*x;
}
return tmp;
}
}
function comb(fn){
return function(y){
if(arguments.length>1){
return fn(arguments[0],arguments[1]);
}
else {
return function(x){
return fn(x,y);
};
}
};
}
var foo = comb(power);
console.log(foo(2,5));
var bar = foo(5);
console.log(bar(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment