Reputation: 167
I have to write a currying function that takes a function, executes another function, then executes a second function by taking the last argument as the value to calculate with.
What I am struggling with: How can I access the first function and the value at the end at the same time?
So far I can access the first function by writing a function in the function syntax and accessing the this.
Function.prototype.foo = function(x) {
// can't make currying functions by using functions keyword
console.log(this, x);
};
(() => 10).foo(1);
When I write a currying function I can access the second (x) and third (y) function.
Function.prototype.bar = x => y => {
// but also can't access this like in function syntax
console.log(x, y);
}
// how would I access 10 in bar?
(() => 10).bar(1)(2);
The final Function would look something like that:
someFunc.then(someSecondFunc).then(someThirdFunc)(100)
Thank you so much for your help!
Upvotes: 1
Views: 62
Reputation: 167
By using currying functions and the function keyword, my answer looks like this:
Function.prototype.then = function(secondFct) {
const firstFct = this;
return function(value) {
return firstFct(secondFct(value));
}
}
Many thanks to buboh for the help.
Upvotes: 0
Reputation: 957
Not sure if it solves your problem, but you can make currying functions with the function keyword:
Function.prototype.bar = function(x) {
return function(y) {
console.log(x, y)
}
}
I wasn’t able to actually able to validate if this works:
(function() {return 10}).bar(1)(2)
In any case, ˋthisˋ would be the function, not the return value (10), since the function is not called.
Upvotes: 1