Currying

What is currying?

A. “Returning a function that takes multiple arguments” into a “chain of functions that takes one argument”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Without Currying
const add = (x, y) => x + y;
console.log(add(1, 2)); // 3

// Currying: Ver 1
const _add = (x) => (y) => x + y;
console.log(_add(1)(2)); // 3

// Currying: Ver 2
const __add = (x) => {
return (y) => {
return x + y;
};
};
console.log(__add(1)(2)); // 3

// Currying: Ver 3
const ___add = function (x) {
return function (y) {
return x + y;
};
};
console.log(___add(1)(2)); // 3

// *Ver 2 and Ver 3 are the same as Ver1. They are just for explanation.