Skip to content

Instantly share code, notes, and snippets.

@rajatjain-21
Last active November 23, 2020 10:36
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 rajatjain-21/f074b635b6a4edb37f95c672f9405843 to your computer and use it in GitHub Desktop.
Save rajatjain-21/f074b635b6a4edb37f95c672f9405843 to your computer and use it in GitHub Desktop.
function curry(func, length = func.length) {
// passing a second paramenter as the number of arguments
// passed to the function
return function (...args) {
if (args.length >= length) {
// if the function call contains arguments which are
// greater than or equal to the max, just the return the func
return func(...args);
} else {
// otherwise call the curry again, binding the already
// appeared arguments and this time, decreasing the length of agruments by
// number of arguments passed earlier
return curry(func.bind(this, ...args), length - args.length);
}
};
}
function add(a, b, c) {
return a + b + c
}
const curryAdd = curry(add);
console.log(curryAdd(3,4,5)) // 12
console.log(curryAdd(4,5)(4)) // 13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment