Created
November 19, 2014 17:15
-
-
Save ecasilla/e82bfc12f1976e0344aa to your computer and use it in GitHub Desktop.
Currying In Js
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
| function curry(fn) { | |
| //create sunt double function to check artiy of the calling function i.e fn | |
| return function() { | |
| //check the length of args and if its greater copy them and return a new function | |
| if (fn.length > arguments.length) { | |
| //copy logic | |
| var slice = Array.prototype.slice; | |
| var args = slice.apply(arguments) | |
| //new function with args copied | |
| return function() { | |
| return fn.apply( | |
| null, args.concat(slice.apply(arguments))); | |
| }; | |
| } | |
| //else just pass the arguments back to the original | |
| return fn.apply(null, arguments); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment