Skip to content

Instantly share code, notes, and snippets.

@AGhost-7
Last active May 23, 2017 03:41
Show Gist options
  • Save AGhost-7/2fa84849539f7a6712fbd1eeb8456b11 to your computer and use it in GitHub Desktop.
Save AGhost-7/2fa84849539f7a6712fbd1eeb8456b11 to your computer and use it in GitHub Desktop.
Function currying similar to lodash's
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