Functional Programming: Introduction to Currying and Partial Application

hello campers ,please could anyone explain to me why the this keyword in this challeng reffers to 10 ,and where partial application is usefull .

//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application/

I’ll explain the this part first because it’s not really that interesting

bind's first argument controls what this means inside the function. this in javascript can refer to many things, depending on the context. bind fixes what this refers to.

this is worth reading about, though it’s a bit confusing, don’t worry if you don’t get it right away.


Alrighty, partial application:

Partial application is used to create functions with some of the parameters “already filled in” - it’s like applying a function, but only applying some of the parameters, hence partially applying a function.

I’ll give a possibly bad example:

Consider making a function that takes a string and creates a new string that represents some valid html fragment:

makeH1(str) {
    return `<h1>${str}</h1>`
}

That’s nice and all, but we can make a more general version:

makeHTMLElement(elementString, str){
    return `<${elementString}>${str}</${elementString}>`;
}

With this we can make arbitrary elements more easily:

makeLi(str) {
    return makeHTMLElement('li',  str);
}

Partial application is essentially taking a function and putting default arguments in.

It comes from a more deeper idea in the sense that all functions of multiple arguments can be thought of as a chain of functions of a single argument.

As to where this is exactly useful, well, it’s hard to come up with concrete examples on the spot for me, but as you learn more functional programming you’ll see it come up a lot.

It’s fairly fundamental for a lot of the theory

1 Like

TL;DR — The four rules presented for determining what this in JavaScript points to boil down to one simple question: What is the calling object?

Precedence of the four rules:
(The JavaScript interpreter will ask in this order)

Is the function called by new?
Is the function called by call(), apply(), or bind()?
Is the function called as a method, ie: obj.func()?
Is the function called in the global scope?
If strict mode is enabled, return undefined.
Otherwise, return the global object, ie: window.

Continue the reading here as “this” dynamic mechanism is thoroughly explained.
‘’'Js

2 Likes

It can occasionally be helpful for cleaning code up, but it’s cumbersome to do in JavaScript and therefore isn’t terribly useful.

In many functional languages, all functions must take one and only one argument, and this makes partial application very easy and very useful because it lets you glue functions together like Lego bricks by passing them to other functions.

Note that there are a set of proposals to add support at a syntax level for partial application. So this may be something that becomes more important if those proposals make into the language. As it is, it’s just a simple and occasionally useful thing to know.

1 Like

thank you for your time .