Created
July 5, 2016 03:59
-
-
Save marty-wang/142281635e1862eb5974f60324db61ce to your computer and use it in GitHub Desktop.
Very small curry function
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
function curry(fn) { | |
return (...args) => { | |
if (args.length >= fn.length) { | |
return fn.apply(null, args) | |
} | |
return function internal(...args1) { | |
args = args.concat(args1) | |
if (args.length >= fn.length) { | |
return fn.apply(null, args) | |
} | |
return internal | |
} | |
} | |
} | |
const curried = curry((a, b, c) => `${a} ${b} ${c}`) | |
console.log(curried("a", "b", "c")) // "a b c" | |
console.log(curried("a", "b")("c")) // "a b c" | |
console.log(curried("a")("b", "c")) // "a b c" | |
console.log(curried("a")("b")("c")) // "a b c" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment