Created
March 5, 2014 21:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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