Last active
May 23, 2017 03:41
-
-
Save AGhost-7/2fa84849539f7a6712fbd1eeb8456b11 to your computer and use it in GitHub Desktop.
Function currying similar to lodash's
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
var argCurry = function(args, n, fn) { | |
if(args.length === n) { | |
return fn.apply(null, args) | |
} else { | |
return function() { | |
var newArgs = args.concat(Array.prototype.slice.call(arguments)) | |
return argCurry(newArgs, n, fn) | |
} | |
} | |
} | |
var curryN = function(n, fn) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments) | |
return argCurry(args, n, fn) | |
} | |
} | |
var curry = function(fn) { | |
var n = arguments.length === 1 ? fn.length : arguments[1] | |
return curryN(n, fn) | |
} | |
var fn1 = curry(function(a) { | |
console.log(a) | |
}) | |
fn1()()(1) | |
var fn2 = curry(function(a, b) { | |
return [a, b] | |
}) | |
console.log(fn2(1)()(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment