Created
June 12, 2017 21:32
-
-
Save smidhonza/66cf258d5280db51d2a551cc23966ffe to your computer and use it in GitHub Desktop.
ES6 Curry
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
export const curry = (fn) => { | |
const r = (args) => { | |
if (args.length >= fn.length) { | |
return fn(...args); | |
} | |
return (...secArgs) => r([...args, ...secArgs]); | |
}; | |
return (...args) => r(args); | |
}; |
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
describe('curry', () => { | |
it('returns a curried sum function', () => { | |
const sum = curry((a, b, c, d) => a + b + c + d); | |
expect(sum(1, 2, 3, 4)).toEqual(10); | |
}); | |
it('returns a function if you miss the params', () => { | |
const sum = curry((a, b, c, d) => a + b + c + d); | |
const sum1 = sum(1); | |
const sum2 = sum1(2); | |
const sum3 = sum2(3); | |
expect(sum3(4)).toEqual(10); | |
}); | |
it('combines the two approaches and still returns correct values', () => { | |
const sum = curry((a, b, c, d) => a + b + c + d); | |
const sum1 = sum(1, 2); | |
const sum2 = sum1(3); | |
expect(sum2(4)).toEqual(10); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment