-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathwinebrenner444_solutions.js
More file actions
193 lines (155 loc) · 3.25 KB
/
winebrenner444_solutions.js
File metadata and controls
193 lines (155 loc) · 3.25 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const identity = x => x;
const addb = (a,b) => a + b;
const subb = (a,b) => a - b;
const mulb = (a,b) => a * b;
const minb = (a,b) => {
if(a > b) {
return a;
} else {
return b;
}
}
const maxb = (a,b) => {
if(a < b) {
return a;
} else return b;
}
const add = (...nums) => {
return nums.reduce(function (acc, cur) {
return acc + cur;
})
}
const sub = (...nums) => {
return nums.reduce(function (acc, cur) {
return acc - cur;
})
}
const mul = (...nums) => {
return nums.reduce(function (acc, cur) {
return acc * cur;
})
}
const min = (...nums) => Math.min(...nums);
const max = (...nums) => Math.max(...nums);
const addRecurse = (...nums) => {
let sum = 0;
for(let i = 0; i < nums.length; i++) {
sum += nums[i];
} return sum;
}
const mulRecurse = (...nums) => {
let product = 1;
for(let i = 0; i < nums.length; i++) {
product *= nums[i];
} return product;
}
const minRecurse = (...nums) => {
let num = nums[0];
for(let i = 0; i < nums.length; i++) {
if(nums[i] < num) {
num = nums[i];
}
} return num;
}
const maxRecurse = (...nums) => {
let num = nums[0];
for(let i = 0; i < nums.length; i++) {
if(nums[i] > num) {
num = nums[i];
}
} return num;
}
const not = (func) => (...args) => {
const result = func(...args);
return !result;
};
const acc = (func, initial) => (...args) => {
let final = initial;
for(let i of args) {
final = func(final, i)
} return final;
}
const accPartial = (func, start, end) => (...args) => {
const array = args.slice(start, end);
let final = array[0];
for (let i of array.slice(1)) {
final = func(final, i);
}
args.splice(start, end - start);
args.splice(start, 0, final)
return args;
}
const accRecurse = (func, initial) => (...args) => {
if (args.length === 1) {
return func(args[0], initial);
}
return func(accRecurse(func, initial)(...args.slice(1)), args[0]);
}
const fill = (num) => {
return new Array(num).fill(num);
}
const fillRecurse = (num) => {
if( typeof num === 'number') {
return new Array(num).fill(num);
} else {
alert("Not a number!")
}
}
const set = (...args) => {
return new set([args]);
}
const identityf = x => x;
const addf = a => b => a + b;
const liftf = fun => a => b => fun(a, b);
const pure = (x, y) => {
const impure = (x) => {
y++;
z = x * y;
}
impure(x);
return [y, z];
}
const curryb = binary => a => b => binary(a + b);
const curry = func => func(...outer);
let val = 1;
let inc = _.inc(val);
let inc2 = val.inc();
const twiceUnary = (biFunc) => (x) => biFunc(x, x);
const addU = (x, y) => x + y;
const doubl = twiceUnary(addU);
const mult = (x, y) => x * y;
const square = twiceUnary(mult);
module.exports = {
identity,
addb,
subb,
mulb,
minb,
maxb,
add,
sub,
mul,
min,
max,
addRecurse,
mulRecurse,
minRecurse,
maxRecurse,
not,
acc,
accPartial,
accRecurse,
fill,
fillRecurse,
set,
identityf,
addf,
liftf,
pure,
curryb,
curry,
inc,
twiceUnary,
doubl,
square,
};