Skip to content

Instantly share code, notes, and snippets.

@marty-wang
Created July 5, 2016 03:59
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 marty-wang/142281635e1862eb5974f60324db61ce to your computer and use it in GitHub Desktop.
Save marty-wang/142281635e1862eb5974f60324db61ce to your computer and use it in GitHub Desktop.
Very small curry function
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