const curry = (f, xs = []) => (...args) => (xs.length + args.length) >= f.length ? f(...xs, ...args) : curry(f, xs.concat(args))
const fn = (f) => (...args) => args.reduce((f, arg) => f(arg), f);// ==================================const sum = fn(a => b => c => (a + b + c));console.log(sum(1, 2, 3)); // 6console.log(sum(1, 2)(3)); // 6console.log(sum(1)(2)(3)); // 6const doubleInc = sum(1)(1);console.log(doubleInc(4)); // 6- const curry = (f, xs = []) => (...args) =>
- (xs.length + args.length) >= f.length
- ? f(...xs, ...args)
- : curry(f, xs.concat(args))
describe("Curring in js", function() { it("works with sum function", function() { const sum = curry((a, b, c) => (a + b + c)); Test.assertEquals(sum(1, 2, 3), 6); Test.assertEquals(sum(1, 2)(3), 6); Test.assertEquals(sum(1)(2)(3), 6); const doubleInc = sum(1)(1); Test.assertEquals(doubleInc(4), 6); }); });
- describe("Curring in js", function() {
- it("works with sum function", function() {
- const sum = curry((a, b, c) => (a + b + c));
- Test.assertEquals(sum(1, 2, 3), 6);
- Test.assertEquals(sum(1, 2)(3), 6);
- Test.assertEquals(sum(1)(2)(3), 6);
- const doubleInc = sum(1)(1);
- Test.assertEquals(doubleInc(4), 6);
- });
- });