Last active
August 29, 2015 14:06
-
-
Save ezsi/1709e12939ca3fd80c49 to your computer and use it in GitHub Desktop.
JavaScript: currying function
This file contains 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
// Currying: f(f1,...,fn) -> f(f1,...,fk)(fk+1,...,fn) | |
// Usage: f.curry(f1,...,fk) -> g(fk+1,...fn) == f(f1,...,fn) | |
Function.prototype.curry = function() { //f(f1,...,fk) | |
if( arguments.length == 0 ) | |
return this; | |
var args = arguments; | |
var fn = this; | |
return function() { // g(fk+1,...,fn) | |
var prefix = Array.prototype.slice.call(args); | |
var suffix = Array.prototype.slice.call(arguments); | |
fn.apply(this, prefix.concat(suffix)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment