Last active
October 23, 2020 01:37
-
-
Save WaldoJeffers/ebc37a2b81c148fcf2cd6b97eae254cb to your computer and use it in GitHub Desktop.
JavaScript one-line curry (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, arity = fn.length) => (...args) => args.length >= arity ? fn(...args) : (...moreArgs) => curry(fn)(...args, ...moreArgs) | |
// tests | |
const _add = (a, b, c) => a + b + c | |
describe('curry', () => { | |
it('should currify the input function', () => { | |
const add = curry(_add) | |
expect(add(1, 2, 3)).toBe(6) | |
expect(add(1)(2)(3)).toBe(6) | |
expect(add(1, 2)(3)).toBe(6) | |
expect(add(1)(2, 3)).toBe(6) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment