Skip to content

Instantly share code, notes, and snippets.

@smidhonza
Created June 12, 2017 21:32
Show Gist options
  • Save smidhonza/66cf258d5280db51d2a551cc23966ffe to your computer and use it in GitHub Desktop.
Save smidhonza/66cf258d5280db51d2a551cc23966ffe to your computer and use it in GitHub Desktop.
ES6 Curry
export const curry = (fn) => {
const r = (args) => {
if (args.length >= fn.length) {
return fn(...args);
}
return (...secArgs) => r([...args, ...secArgs]);
};
return (...args) => r(args);
};
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