Last active
December 16, 2015 00:59
-
-
Save schell/5351336 to your computer and use it in GitHub Desktop.
Function currying using javascript.
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 curry(f, arg) { | |
// Return a new curried function. Mmmm, spicy. | |
return function curriedFunction() { | |
// Take the other arguments and join them with our | |
// curried arg. | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(arg); | |
// Apply the function. | |
return f.apply(null, args); | |
}; | |
} | |
// Example: | |
function concatStrings(front, back) { | |
return front + back; | |
} | |
var prefixWithFoo = curry(concatStrings, "Foo"); | |
console.log(prefixWithFoo("Bat")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment