Created
March 26, 2018 08:26
-
-
Save lqt0223/50573a7374a66cc72808c539d0041e54 to your computer and use it in GitHub Desktop.
30 currify a function
This file contains 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 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