-
Notifications
You must be signed in to change notification settings - Fork 1
/
currying.js
87 lines (71 loc) · 1.75 KB
/
currying.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 4 args
const sum = (...args) => {
//spread the arguments in storage array
const storage = [...args];
//base case
if(storage.length === 4){
return storage.reduce((a, b) => a + b, 0);
}
}
/**
* Curry using a sum and when the function is called internally.
* let curriedSum = curry(sum);
* console.log(curriedSum(1)(2)(4)); // 3
*/
//
function curry(f) {
// curry(f) does the currying transform
return function curried(...args) {
if (args.length >= f.length) {
return f.apply(this, [...args]);
} else {
return function (...args2) {
return curried.apply(this, [...args, ...args2]);
};
}
};
}
// usage
function sum(a, b, c) {
return a + b + c;
}
let curriedSum = curry(sum);
console.log(curriedSum(1)(2)(4)); // 3
/// Added sum for empty argument.
function sum(...args) {
if (args.length === 0) {
return args.reduce((a, b) => a + b);
} else {
return function (...args2) {
let tempArr = [...args, ...args2];
if (args2.length === 0) {
return tempArr.reduce((a, b) => a + b, 0);
}
return sum(...tempArr);
};
}
}
console.log(sum(1)(2)(3)(4)()); // Output: 10
console.log(sum(1)(2, 3)(4)()); // Output: 10
/*Custom method with sum that triggers currying with empty arguments*/
function curry(fn) {
return function (...args) {
let storage = [...args];
if (storage.length === 0) {
console.log("h");
return fn.apply(this, storage);
} else {
return function curried(...args2) {
if (args2.length === 0) {
return fn.apply(this, storage);
} else {
storage = [...storage, ...args2];
return curried;
}
};
}
};
}
let sum = (a, b) => a + b;
let c = curry(sum);
console.log(c(1)(2)());