Skip to content

Instantly share code, notes, and snippets.

@cmstead
Created October 17, 2014 17:26
Show Gist options
  • Save cmstead/8dc9f49712f27b376293 to your computer and use it in GitHub Desktop.
Save cmstead/8dc9f49712f27b376293 to your computer and use it in GitHub Desktop.
Currying example in JS
(function(){
var collection = [1, 2, 3, 4, 5, 6, 7],
add10,
newCollection;
function map(passedCollection, userFn){
var newCollection = [];
passedCollection.forEach(function(value){
newCollection.push(userFn(value));
});
return newCollection;
}
function curry(userFn){
var initialArgs = Array.prototype.slice.call(arguments, 1);
return function(){
return userFn.apply(null, initialArgs.concat(arguments[0]));
}
}
function add(a, b){
return a + b;
}
add10 = curry(add, 10);
newCollection = map(collection, add10);
console.log(newCollection);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment