-1

I am trying to implement currying pattern for sum multiplication in my simple program. For that, I have defined one generic curry function and calling sum and multiply functions in it but I am not getting any output. can anyone tell me what's wrong in my program? Thanks in advance.

code ::

function curry(fn) {
    sum.call(this, a,b);
    multiply.call(this, a,b,c);
}

function sum(a, b) {
    return a + b;
}

function multiply(a, b, c) {
    return a * b * c;
}

const curriedSum = curry(sum);
const curriedMult = curry(multiply);

const addOne = curriedSum(1);
const addTwo = curriedSum(2);

Expected Output ::

// 1 + 3 = 4
console.log('result', addOne(3));      // 'result'   4


// 1 + 1 = 2
console.log('result', addOne(1));     // 'result'   2


// 2 + 5 = 7
console.log('result', addTwo(5));     // 'result'   7


// 2 * 3 * 4 = 24
console.log('result', curriedMult(2)(3)(4));    // 'result'   24
Riya
  • 415
  • 9
  • 23
  • What error are you seeing? – Tad Donaghe Oct 17 '18 at 22:53
  • I am not getting any output – Riya Oct 17 '18 at 22:53
  • 1
    Your `curry` function does not curry anything. It also tries to call `sum` with two undefined values, since `a` and `b` are only defined in the actual `sum` function, not the `call`. – Heretic Monkey Oct 17 '18 at 22:53
  • 1
    `curry` doesn't have a `return` statement so it returns `undefined` by default (but as Heretic Monkey said, it actually doesn't finish execution because `a` and `b` are not declared). – Felix Kling Oct 17 '18 at 22:54
  • Please see [What is 'Currying'?](https://stackoverflow.com/q/36314/215552) for examples of curried functions. – Heretic Monkey Oct 17 '18 at 22:56
  • I want to define currying for sum and multiplication that's why I added call in curry function – Riya Oct 17 '18 at 22:58
  • I am trying to implement generic method which does both sum and multiplication. – Riya Oct 17 '18 at 23:04
  • @Riya the answer you picked isn't currying anything it's just making a function that calls the function passed in. In that answer `addOne` isn't a function it's just a number. – Mark Oct 18 '18 at 18:36

2 Answers2

2

I think you should take a step back and ask what you want that curry function to return. The way you are using it suggests you want it to return a function, so it needs to at least do that. That function looks like it should also take arguments, because you are using it that way.

So…just return a function that takes arguments and binds them to the function you passed into curry and return that.

const curry = (fn) => (...a) => fn.bind(null, ...a)

This says curry is a function that takes a function parameter (fn) and returns a new function that takes arguments and returns yet another function that is the result of fn.bind

To use it you can:

const curry = (fn) => (...a) => fn.bind(null, ...a)

function sum(a, b) {
    return a + b;
}

function multiply(a, b, c) {
    return a * b * c;
}

const curriedSum = curry(sum);
const curriedMult = curry(multiply);

const addTwo = curriedSum(2);
console.log(addTwo(10))              // 2 + 10

const mult10 = curriedMult(10)
// because we spread the args in curry you 
// can pass two arguments
console.log(mult10(10, 2))           // 10 * 10 * 2

// or you can even curry it again:
const mult20 = curriedMult(20)
const curriedMult20 = curry(mult20)
const mult20_10 = curriedMult20(10)
console.log(mult20_10(2))            // 20 * 10 * 2
Mark
  • 90,562
  • 7
  • 108
  • 148
2

On your curry function the problem is you dont invoke the fn that you are passing, you must keep the fn in closure, and return new function which waits to second paremater to invoke itself.

You might want to check this great article about clousers;

Also here is the working example of your code;

const curry = (fn) => (...args) => fn(...args)

function sum(a, b) {
  return a + b;
}

function multiply(a, b, c) {
  return a * b * c;
}

const curriedSum = curry(sum);
const curriedMult = curry(multiply);

const addOne = curriedSum(1,2); 
const addTwo = curriedSum(2,3); 
const multiple = curriedMult(2,3,4) 

console.log(addOne)
console.log(addTwo)
console.log(multiple)
Halil Irmak
  • 1,271
  • 1
  • 13
  • 24
  • This isn't actually [currying](https://en.wikipedia.org/wiki/Currying) anything. It's just wrapping the function in a a new function. The value you are saving in `addOne` is just the result of `1+2` not a new function that adds one to the argument. – Mark Oct 18 '18 at 18:34