3

I'm currently learning JavaScript and got stuck by adding a parameter to my function that should add two numbers. I'm not new to programming. Did a lot in Java and C#, but Javascript drives me crazy with scope and compose and currying.... So here is my problem:

const compose = (f, g) => (a) => f(g(a));
const add1 = (num) => num + 1;
const add5 = (num) => num + 5;
compose(add1, add5)(10)          // 16

The code above works! But i want that the +1 and +5 number to be variable so that i can have like this:

// Something linke this 
const compose = (f, g) => (a,b) => f(g(a,b));
const add1 = (num, num2) => num + num2;
const add5 = (num, num2) => num + num2;
compose(add1, add5)(10, 4)   // Those numbers should be the Parameter input for add1 and add5 (num, num2) 
6
  • You seem to be making this very complicated, what's wrong with just creating a function compose(){...}? No one gives extra marks for making things complicated Commented Oct 28, 2019 at 11:51
  • 1
    What would you expect to be returned here? Both functions expect two arguments, if you want to pass same numbers to both functions, how do you compose those? Commented Oct 28, 2019 at 11:55
  • 1
    In f(g(a, b)), g(a, b) is 1st parameter and 2nd parameter is missing Commented Oct 28, 2019 at 11:57
  • 1
    I am not trying to make things complicated, i just want to really really understand how to add a parameter to my compose method and make it variable and not hardcoding some numbers. Commented Oct 28, 2019 at 11:57
  • 1
    Expected output? Commented Oct 28, 2019 at 12:00

2 Answers 2

3

Maybe like this:

    const addN = (n) => num => num + n
    const compose = (f,g) => a => f(g(a));
    const result = compose(addN(10),addN(4))(1);
    console.log(result);

addN creates adder functions, in this case an add10 and an add4. Instead of returning a value as your original add1 and add5 did, addN is returning a function like add1 and add5 except it isn't hardcoded to be +1 or +5; it is + whatever you passed in when you called addN.

console.log(addN(6)(10)); //16

addN(6) creates an "add6" function, and then we call that "add6" function with 10 to get the output of 16

Sign up to request clarification or add additional context in comments.

3 Comments

@ Anthony thank youuuuuuuuuu! That's it. Could you please explain it a liitle ?
@Codex0607 I put a little more in, hopefully that helps!
glad there are such community like Stackoverflow !!!! Thank you all guys ! Sorry Anthony, i cant give you a upvote, cuz i'm a new user. again thanks!!!!!!! Edit: Now i got my Upvotes priviliges and you got one from me! BTW....your explanation is very newbie friendly
0

You can also use curried function like this to achieve the same in a better / simpler way without composing it.

// add = a => b => Number
const add = a => b => a + b;

const result = add(2)(3); // => 5

First, the function takes a, and then returns a new function, which then takes b returns the sum of a and b. Each argument is taken one at a time. If the function had more parameters, it could simply continue to return new functions until all of the arguments are supplied and the application can be completed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.