Okay so could someone help me out. I passed this test. I read an explanation of currying and partial application on another website. I don’t understand how the function knows to add the numbers rather than perform some other operator? See the example below from this lesson and an additional example.
**Example code **
//Impartial function
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
One more example from another site:
function volume(l,h,w) {
return l * h * w
}
const hCy = curry(volume,100);
hCy(200,900); // 18000000l
hCy(70,60); // 420000l
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.
Challenge: Introduction to Currying and Partial Application
If x, y, and z are of the type number, then the + operator means addition.
If they were strings, it would concatenate the strings.
If it was a mix of numbers and strings, then it would work from left to right. Once it encountered a string, the numbers would be converted to strings and it would concatenate.
Okay so the word this related to the number 10 in partialFn(10);?
The second example confuses me more because I don’t understand how 100 is being multiplied. Is it that the values here hCy(200,900); fill in the volume so that it looks like hCy(200,900, 100)?
No, it doesn’t matter, in this context it’s irrelevant, it can be null or undefined. bind’s primary use is to borrow or to pass around a function attached to an object. The first argument is the object context (this). You aren’t doing this, you’re just using bind to partially apply arguments, so it doesn’t matter what you put as the first argument.
The second argument to bind is whatever the first argument to the bound function is, the third argument to bind is the second argument (and so on and so forth).
You’ve missed out the definition of the function, so it’s very difficult to see what it is. It doesn’t look like currying, it looks like partial application, I would guess like (sorry this is off the top of my head, it’s not tested and might not work properly):
function curry(fn, param) {
return function (args) {
return fn.call(this, param, ...args);
}
}
function takes a function + the first argument to the curried function
this returns a function that takes some arguments (these will be the remaining arguments)
this executes the original function. call is quite similar to bind, but it runs a function: it has the same first argument, then you pass the rest of the arguments