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 ?
CodePudding user response:
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
CodePudding user response:
You can forward arguments to a function like this:
const twice = x => x * x;
const invokeFunctionWithArgs = (fn, ...args) => fn(...args);
console.log(invokeFunctionWithArgs(twice, 5)); // 25