Created
June 25, 2015 23:33
-
-
Save gowon/d9f50c0506c888a73aae to your computer and use it in GitHub Desktop.
Javascript Currying Example
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
| // Example of a currying methods | |
| // https://medium.com/@kbrainwave/currying-in-javascript-ce6da2d324fe | |
| var currier = function(fn) { | |
| var args = Array.prototype.slice.call(arguments, 1); | |
| return function() { | |
| return fn.apply(this, args.concat( | |
| Array.prototype.slice.call(arguments, 0))); | |
| }; | |
| }; | |
| // example function | |
| var sequence = function(start, end) { | |
| var result = []; | |
| for(var i = start; i <= end; i++) { | |
| result.push(i); | |
| } | |
| return result; | |
| }; | |
| //Example use of currying | |
| var seq5 = currier(sequence, 1); | |
| seq5(5); // [1, 2, 3, 4, 5]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment