Created
October 17, 2014 17:26
-
-
Save cmstead/8dc9f49712f27b376293 to your computer and use it in GitHub Desktop.
Currying example in JS
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
(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