Skip to content

Instantly share code, notes, and snippets.

@sunderls
Created June 1, 2020 15:48
Show Gist options
  • Save sunderls/a7b118b72d97f9d69f9d3e1a97da4fc5 to your computer and use it in GitHub Desktop.
Save sunderls/a7b118b72d97f9d69f9d3e1a97da4fc5 to your computer and use it in GitHub Desktop.
curryify
function curry(fn) {
// fn(1,2,3) => fn(1)(2,3) => fn(1,2)(3)
// so it return a function which:
// 1. if enough param, just run it
// 2. if not enough param, return another function with param prefilled
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(null, args)
} else {
return curried.bind(null, ...args)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment