Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to pass argument to multiple lambda expressions in javascript?

can i give 2 argumens if there is multiple lambda funcionts in javascripts?
i have the following code

var twice = (x) => {
  return x *x;
}

var lambda = (b) => { 
  (a) =>{
    return b(b(a));
  }
}; 


console.log(lambda(twice, 5));

this wont work, and
I could use

  var lambda = (b,a) => { 
        return b(b(a));
    }; 

and will work, but can I use separate lambda expression and give them arguments ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, You would take advantage of closures so that the inner function returned from invoking lambda() can keep a reference to its surrounding lexical environment.

You then call said inner function with the desired value lambda(twice)(5), in this case, 5.

var twice = (x) => {
  return x *x;
}

var lambda = (b) => { 
  return (a) => {
    return b(b(a));
  }
}; 


console.log( lambda(twice)(5) ); // -> 625
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading