-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionCurrying.js
More file actions
59 lines (42 loc) · 1.83 KB
/
functionCurrying.js
File metadata and controls
59 lines (42 loc) · 1.83 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Function Currying is one of the most asked Interview Question.
* Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions.
* It returns a new function that expects the next argument inline.
* In other words, when a function instead of taking all arguments at one time, takes the first one and return a new function that takes the second one
* and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.
* Concept of closures is also come into picture as closures remember the outer scope varaible even though the outer function is returned.
*/
function sum(x) {
return function (y) {
return x + y;
}
}
console.log(sum(11)(22));
// Using Fat Arrow Functions
const multiply = x => y => z => x * y * z;
console.log(multiply(1)(2)(3));
function subtraction(a) {
return (b, c) => {
return a * b * c
}
}
console.log(subtraction(10)(11, 22)); // This is not an example of function currying as the function will take single argument at a time & and its a example of "Partial Applied function"
let users = {
firstName: 'Yamini',
lastName: 'Kota',
city: 'Andhra Pradesh'
}
// without currying
function getUserDetail(data, key) {
return data[key];
}
console.log(getUserDetail(users, 'lastName'));
console.log(getUserDetail(users, 'city'));
console.log(getUserDetail(users, 'firstName')); // here we are sending 'users' object each and every time..to make 'users' fixed
// Using Function Currying
function getData(data) {
return key => data[key]
}
const userData = getData(users); // here we are sending 'users' object only one time
console.log(userData('firstName')); // then sending the keys as parameters into the function
console.log(userData('lastName'));