Skip to content

Instantly share code, notes, and snippets.

@lqt0223
Created March 26, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lqt0223/50573a7374a66cc72808c539d0041e54 to your computer and use it in GitHub Desktop.
Save lqt0223/50573a7374a66cc72808c539d0041e54 to your computer and use it in GitHub Desktop.
30 currify a function
var curry = (f) => {
var argc = f.length
var _curry = (fn) => {
return (...args) => {
if (args.length == argc) {
return fn(...args)
} else {
return fn.bind(null, ...args)
}
}
}
for (var i = 0; i < argc - 1; i++) {
f = _curry(f)
}
return f
}
// test
var arith = (a, b, c, d, e) => a + b - c * d / e
var carith = curry(arith)
console.log(carith(1)(2)(3)(4)(5))
console.log(carith(1, 2)(3)(4)(5))
console.log(carith(1, 2, 3)(4)(5))
console.log(carith(1)(2, 3)(4, 5))
console.log(carith(1, 2, 3)(4, 5))
console.log(carith(1, 2, 3, 4, 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment