Skip to content

Instantly share code, notes, and snippets.

@JenniferFuBook
Last active September 5, 2019 23:28
Show Gist options
  • Save JenniferFuBook/b4e0d9a4bf2c2ce2da930208525e0cca to your computer and use it in GitHub Desktop.
Save JenniferFuBook/b4e0d9a4bf2c2ce2da930208525e0cca to your computer and use it in GitHub Desktop.
Currying is about decomposing a function taking multiple arguments into numerous functions with single arguments. It is named after Haskell Curry.
const curry = fn => {
if (fn.length === 0) {
return fn;
}
const _curry = (...args) => {
if (fn.length <= args.length) {
return fn.apply(null, args);
}
return _curry.bind(null, ...args);
}
return _curry;
}
const add = (a,b,c,d) => a + b + c + d;
curry(add)(1,2)(3)(4); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment