Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created May 23, 2016 21:09
Show Gist options
  • Save remarkablemark/a8d7e55d3169b10762f90bfac955c028 to your computer and use it in GitHub Desktop.
Save remarkablemark/a8d7e55d3169b10762f90bfac955c028 to your computer and use it in GitHub Desktop.
Example of currying in JavaScript.
'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