Skip to content

Instantly share code, notes, and snippets.

@pratyushcrd
Created August 14, 2017 07:41
Show Gist options
  • Save pratyushcrd/498da9cf56a2cdb30c67187c5c083e6a to your computer and use it in GitHub Desktop.
Save pratyushcrd/498da9cf56a2cdb30c67187c5c083e6a to your computer and use it in GitHub Desktop.
One line curry function in Javascript ES6
const curry = (fn, arr = []) => (...params) => (arr.length + params.length >= fn.length) ? fn(...[...arr, ...params]) : curry(fn, [...arr, ...params])
/* Testing with a sum function */
const sum = curry(function (a, b, c, d, e) {
return a + b + c + d + e
})
console.log(sum(1, 2, 3, 4, 5)) // 15
console.log(sum()()(1)()(2)()(3, 4)(5)) // 15
console.log(sum(1, 2, 3)()()()(4, 5)) // 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment