Created
May 23, 2016 21:09
-
-
Save remarkablemark/a8d7e55d3169b10762f90bfac955c028 to your computer and use it in GitHub Desktop.
Example of currying in JavaScript.
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
'use strict'; | |
/** | |
* Curry the function. | |
* | |
* @param {Function} func - The function to curry. | |
* @param {...*} | |
* @return {Function} | |
*/ | |
function curry(func) { | |
var slice = Array.prototype.slice; | |
var args = slice.call(arguments, 1); | |
return function() { | |
return func.apply( | |
this, args.concat(slice.call(arguments, 0)) | |
); | |
}; | |
} | |
/** | |
* Console log helper. | |
* | |
* @param {...*} | |
*/ | |
function say() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
console.log(args.join('')); | |
} | |
var sayHello = curry(say, 'hello'); | |
sayHello(' world'); // hello world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment