-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargumentExercises.js
More file actions
105 lines (94 loc) · 1.99 KB
/
argumentExercises.js
File metadata and controls
105 lines (94 loc) · 1.99 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// function sum() {
// const args = Array.from(arguments);
// let total = 0;
//
// args.forEach((el) => {
// total += el;
// });
//
// return total;
// }
//
// function sum2(...args) {
// let total = 0;
//
// args.forEach((el) => {
// total += el;
// });
//
// return total;
// }
//
// console.log(sum(1, 2, 3, 4) === 10);
// console.log(sum(1, 2, 3, 4, 5) === 15);
// console.log(sum2(1, 2, 3, 4) === 10);
// console.log(sum2(1, 2, 3, 4, 5) === 15);
// Function.prototype.myBind = function(context, ...bindArgs) {
// return (...callArgs) => {
// this.apply(context, bindArgs.concat(callArgs));
// };
// };
//
//
// Function.prototype.myBind2 = function(context) {
// const that = this;
// const bindArgs = Array.from(arguments).slice(1);
//
// return function() {
// const callArgs = Array.from(arguments);
// that.apply(context, bindArgs.concat(callArgs));
// };
// };
//
//
// function curriedSum(numArgs) {
// const numbers = [];
//
// function _curriedSum(num) {
// numbers.push(num);
//
// if (numbers.length === numArgs) {
// let result = 0;
// numbers.forEach((el) => {
// result += el;
// });
//
// return result;
// }
// return _curriedSum;
// }
//
// return _curriedSum;
// }
//
// const sum = curriedSum(4);
// console.log(sum(5)); // => 56
// console.log(sum(30)(20)(1)); // => 56
Function.prototype.curry = function(numArgs) {
const args = [];
const _curry = (arg) => {
args.push(arg);
if (args.length === numArgs) {
return this.apply(null, args);
} else {
return _curry;
}
};
return _curry;
};
Function.prototype.curry2 = function(numArgs) {
const args = [];
const _curry = (arg) => {
args.push(arg);
if (args.length === numArgs) {
return this.call(null, ...args);
} else {
return _curry;
}
};
return _curry;
};
function sumThree(num1, num2, num3) {
return num1 + num2 + num3;
}
console.log(sumThree.curry(3)(4)(20)(6)); // == 30);