-5

Give an example how "currying" might be used in JavaScript:

const component = (argument1) => (argument2) => {
   // Do something
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Michael Niño
  • 437
  • 5
  • 19
  • 4
    May want to take a look at [currying](https://en.wikipedia.org/wiki/Currying) – CRice May 15 '18 at 22:29
  • This is a *primarily opinion-based* question because you're going to get a lot of **opinions** based of experiences, Etc. Therefore, this question should be closed. – Ele May 15 '18 at 22:31
  • Here is an example of such a usage but I do not understand what is the importance of it's use: https://youtu.be/LTunyI2Oyzw?list=PL8V1empgrpJb--IOBFEY_54SHF-Ft5SAa&t=355 – Michael Niño May 15 '18 at 22:33
  • @CRice Thank you. That is exactly the term being used in video – Michael Niño May 15 '18 at 22:34
  • You can say that about absolutely all abstractions. Why do we need functions? Why do we need objects? Why don't we all just do all our work in Brainf*ck. If you ever used `Array.prototype.map` and want all elements to be incremented by a value passing it a arrow function that does this is fine. If you see you do the same several places yu might want to abstract creating that function to a function that takes the values that differ and return a function you can pass to `map`. The nice thing is that you can give that operation a name while as a arrow function it might not show the intent. – Sylwester May 15 '18 at 23:54

4 Answers4

2

That is called function currying. It is useful for implementing the decorator pattern among others.

For example, let's say we start with a simple function add.

const add = (a, b) => a + b;

console.log(add(1, 2));

Now, let's say we want to add two numbers but also add ten every time.

We could do this:

const add = (a, b) => a + b + 10;

console.log(add(1, 2));

But, then we lose our original function.

So we use function currying to do both:

// We keep the original add function
const add = (a, b) => a + b;

// And create another function that returns the modified function
const addTen = (addFunction) => (a, b) => 10 + addFunction(a, b);

// then we store the new function in a different variable
const addPlusTen = addTen(add);

// ...and call the returned function

console.log(add(1, 2));
console.log(addPlusTen(1, 2));

// This becomes useful when we need to make an addPlusFive, addPlusSix and addPlusSeven functions:

const addFive = (addFunction) => (a, b) => 5 + addFunction(a, b);
const addSix = (addFunction) => (a, b) => 6 + addFunction(a, b);
const addSeven = (addFunction) => (a, b) => 7 + addFunction(a, b);

const addPlusFive = addFive(add);
const addPlusSix = addSix(add);
const addPlusSeven = addSeven(add);

console.log(addPlusFive(1, 2));
console.log(addPlusSix(1, 2));
console.log(addPlusSeven(1, 2));
Jonathan Rys
  • 1,782
  • 1
  • 13
  • 25
1

In your code there are two arrow functions, one returns the other which can help you to maintain state/scope using Closure

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

const multiplier = (argument1) => (argument2) => {
   return argument2 * argument1;
};

const multiples8 = multiplier(8);

console.log(multiples8(5));
console.log(multiples8(6));
console.log(multiples8(7));
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
1

What you are doing is called chaining.

const add = (a) => (b) => {
    return a + b;
}

var add5 = add(5); // returns partially applied function
var result = add5(4); // which can be invoked with its own parameter

console.log(result); // 9

Arrow function provides cleaner syntax since if there is one parameter you can omit parenthesis around the arguments, and if function body is single line you can omit the square brackets. We could write same code using regular js function syntax:

const add = function(a){
    return function(b){
    return a + b
  }
}
snnsnn
  • 10,486
  • 4
  • 39
  • 44
-1

Your code have short versions of two nested arrow functions. Here is how full version looks (with return statement and brackets it should looks more clear):

let component = (argument1) => { 
   // console.log(argument1);
   return (argument2) => {
       // console.log(argument2);
   }
}

component('aa')('bb');

First function (with argument1) return FUNCTION. That returned function take argument2.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345