Created
June 19, 2018 15:38
-
-
Save luqmaan/1d43a9657a45a7c310bba25ec3b4a106 to your computer and use it in GitHub Desktop.
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
function curry(func, ...argv) { | |
let prevArgs = argv; | |
function innerCurry(...args) { | |
prevArgs.push(...args) | |
if (prevArgs.length === func.length) { | |
return func(...prevArgs); | |
} | |
return innerCurry; | |
} | |
return innerCurry; | |
} | |
function add1(a) { | |
return a + 1; | |
} | |
function add(a, b, c, d) { | |
return a + b + c + d; | |
} | |
function concat(a, b, c) { | |
return `${a} ${b} ${c}`; | |
} | |
function hi() { | |
return 'hi'; | |
} | |
console.log(curry(add)(1)(4, 2)(3)) | |
console.log(curry(concat)('a', null)()(null)) | |
console.log(curry(hi)()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment