Step by step explanation request for curried functions

Tell us what’s happening:
How does the portion of code denoted with comments “// Call a curried function in parts:” print 3?
I need a step by step explanation please.

Your code so far

//Un-curried function
function unCurried(x, y) {
  return x + y;
}

//Curried function
function curried(x) {
  return function(y) {
    return x + y;
  }
}
//Alternative using ES6
const curried = x => y => x + y

curried(1)(2) // Returns 3

// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Introduction to Currying and Partial Application

Link to the challenge:

Tell us what’s happening:
For the code below, it only talks about impartial functions.
Is the later part the “partial function”?

Please also explain in step-by-step the portion of
code that is “var partialFn = impartial.bind(this, 1, 2);”

Your code so far

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


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Introduction to Currying and Partial Application

Link to the challenge:

The curried function returns a function.

The function being returned is using the x parameter from its outer scope. The only way it can do that is to remember the value that was passed to x for when it (the function that was returned) is called. It does so using a closure.

function curried(x) {
  return function(y) {
    return x + y;
  }
}
const funcForY = curried(1);
console.log(funcForY);
/*
  ƒ (y) {
    return x + y;
  }
*/

Conceptually, you might think of the returned function as having replaced the x parameter with its value, i.e. the argument that was passed to curried (1 in this case).

// funcForY
function (y) {
  return 1 + y;
}

So when you call funcForY with its own argument it returns the sum of the initial argument passed to the curried function and its own argument.

funcForY(2) // 3

That is 1 + 2

1 is the initial argument to curried and 2 is the argument passed into funcForY.


bind returns a new function (bound function) with the this keyword set to the provided value. It also takes a list of pre-specified initial arguments (partially applied function).

function impartial(x, y, z) {
  console.log(x);
  console.log(y);
  console.log(z);
  return x + y + z;
}
// initial arguments, 1 and 2
const partialFn = impartial.bind(this, 1, 2);
console.log(partialFn);
/*
ƒ impartial(x, y, z) {
  return x + y + z;
}
*/
console.log(partialFn(10));
// 1
// 2
// 10
// 13

That is 1 + 2 + 10

1 and 2 are the initial arguments when creating the bound function and 10 is the argument passed into partialFn.

1 Like