Created
August 14, 2017 07:41
-
-
Save pratyushcrd/498da9cf56a2cdb30c67187c5c083e6a to your computer and use it in GitHub Desktop.
One line curry function in Javascript ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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