-
Notifications
You must be signed in to change notification settings - Fork 0
/
028-Currying-Function.js
56 lines (48 loc) · 1.8 KB
/
028-Currying-Function.js
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
/*
Author : Jaydatt Patel
Currying Method Function:
currying is a functional programming technique that involves breaking down a function that takes multiple arguments into a series of functions that take one argument each. This creates a chain of functions, where each function returns another function until the final result is achieved.
*/
console.log("----------------1-------------------");
// Currying Method Function:
function add(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
console.log("add(1,2,3) :", add(1)(2)(3)); //parsing argument with inner function
var method = add("Method"); //parsing argument and return function
var methD = method("-"); //parsing argument and return function
console.log("methD(1) :", methD(1)); //parsing argument and return value
console.log("methD(2) :", methD(2)); //parsing argument and return value
console.log("----------------2-------------------");
// Currying Method Function:
function power(a) {
return function (b) {
return b ** a;
};
}
console.log("power(3)(2):", power(3)(2));
var square = power(2);
console.log("square(2):", square(2));
console.log("square(3):", square(3));
var cube = power(3);
console.log("cube(2):", cube(2));
console.log("cube(3):", cube(3));
console.log("----------------3-------------------");
function pizzaPricing(size) {
return function (topping) {
return function (quantity) {
let amount = 0;
if (size.toLowerCase() === "large") amount += 12;
if (size.toLowerCase() === "medium") amount += 10;
if (size.toLowerCase() === "small") amount += 8;
amount = (amount + 1.5 * topping.length) * quantity;
return amount;
};
};
}
var pizza = pizzaPricing("large")(["bacon", "cheese"]);
console.log(pizza(2));