-
Notifications
You must be signed in to change notification settings - Fork 0
/
musagenius345.ts
253 lines (193 loc) · 5.67 KB
/
musagenius345.ts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
export const identity = (x: number): number => x;
export const addb = (a: number, b: number) => a + b;
export const subb = (a: number, b: number) => a - b;
export const mulb = (a, b) => a * b;
export const minb = (a, b) => Math.min(a, b);
export const maxb = (a: number, b: number) => Math.max(a, b);
// export const add = (...nums) => nums.reduce((sum, num) => sum + num, 0)
//
export const add = (...nums) => nums.reduce((sum, num) => sum + num, 0);
export const sub = (...nums) => nums.reduce((diff, num) => diff - num);
export const mul = (...nums) => nums.reduce((product, num) => product * num);
export const min = (...nums) => Math.min(...nums);
export const max = (...nums) => Math.max(...nums.flat(Infinity));
export const addRecurse = (...nums) => {
if (nums.length === 0) {
return 0;
} else {
const [first, ...rest] = nums;
return first + addRecurse(...rest);
}
};
export const mulRecurse = (...nums) => {
if (nums.length === 0) {
return 0;
} else if (nums.length === 1) {
return nums[0];
} else {
const [first, ...rest] = nums;
return first * mulRecurse(...rest);
}
};
export const minRecurse = (...nums) => {
if (nums.length === 0) {
return NaN;
}
const [first, ...rest] = nums;
const minRest = rest.length > 0 ? minRecurse(...rest) : NaN;
return isNaN(minRest) || first < minRest ? first : minRest;
};
export const maxRecurse = (...nums) => {
if (nums.length === 0) {
return NaN;
} else if (nums.length === 1) {
return nums[0];
} else {
const [first, ...rest] = nums;
const maxRest = maxRecurse(...rest);
return Math.max(first, maxRest);
}
};
export const not =
(func) =>
(...args) =>
!func(...args);
export const acc =
(func, intial = 0) =>
(...args) =>
args.reduce(func, intial);
export const accPartial =
(func, start, end) =>
(...args) => {
const subset = args.slice(start, end);
const result = func(...subset);
const beginning = args.slice(0, start);
const ending = args.slice(end);
const accumulatedArgs = beginning.concat(result).concat(ending);
return accumulatedArgs;
};
export const accRecurse = (func, initial = 0) => {
const recurse = (accumulator, [currentArg, ...restArgs]) =>
currentArg === undefined
? accumulator
: recurse(func(accumulator, currentArg), restArgs);
return (...args) => recurse(initial, args);
};
const fill2 = (num) => Array(num).fill(num);
export const fill = (num) => Array.from({ length: num }, () => num);
function fillRecurse_2(num, array = []) {
if (array.length >= num) {
return array;
}
array.push(num);
return fillRecurse_2(num, array);
}
function fillRecurse_3(num, array = []) {
if (array.length >= num) {
return array;
}
return fillRecurse_3(num, array.concat(num));
}
function fillRecurse_4(num, index = 0) {
if (index >= num) {
return [];
}
return [num].concat(fillRecurse_4(num, index + 1));
}
export function fillRecurse(num, current = num, array = []) {
if (current <= 0) {
return array;
}
return fillRecurse(num, current - 1, [...array, num]);
}
export const set = (...args) => [...new Set(args)];
export const identityf = (arg) => arg;
export const addf = (a: number) => (b: number) => a + b;
export const liftf = (binary: Function) => (a: number) => (b: number) =>
binary(a, b);
export const pure = (x: number, y: number = 5, z: number) => {
function impure(x: number) {
y++;
z = x * y;
}
impure(x);
return [y, z];
};
export const curryb = (binary: Function, num1: number) => (num2: number) =>
binary(num1, num2);
export const curry =
(func: Function, ...outer: number[]) =>
(...inner: number[]) =>
func(...outer, ...inner);
export const inc = (x: number) => x + 1;
export const twiceUnary = (binary: Function) => (x: number) => binary(x, x);
export const doubl = twiceUnary((a: number, b: number) => a + b);
export const square = (num: number) => twiceUnary(mulb)(num);
export const twice =
(func: Function) =>
(...num: number[]) =>
func(...num) * 2;
export const reverseb = (func: Function) => (a: number, b: number) =>
func(b, a);
export const reverse =
(func: Function) =>
(...args: number[]) =>
func(...args.reverse());
export const composeuTwo =
(unary1: Function, unary2: Function) => (arg: number) =>
unary2(unary1(arg));
export const composeu = (...funcs: Function[]) =>
funcs.reduce((accumulator, func) => (x: number) => func(accumulator(x)));
export const composeb =
(binary1: Function, binary2: Function) => (a: number, b: number, c: number) =>
binary2(binary1(a, b), c);
export const composeTwo =
(func1: Function, func2: Function) =>
(...args: any[]) =>
func2(func1(...args));
export const compose =
(...functions: Function[]) =>
(...args: any[]) =>
functions.reduce((result, fn) => [fn(...result)], args)[0];
export const limitb = (binary: Function, lmt: number) => {
let count = 0;
return (a: number, b: number) => {
if (count < lmt) {
count++;
return binary(a, b);
}
return undefined;
};
};
export const limit = (binary: Function, lmt: number) => {
let count = 0;
return (...nums: number[]) => {
if (count < lmt) {
count++;
return binary(...nums);
}
return undefined;
};
};
export function* genFrom(x: number = 0): Generator<number, void, unknown> {
//
while (true) {
yield x++;
}
}
export function* genTo<T>(gen: Generator<T>, lmt: number) {
let count = 0;
for (const value of gen) {
if (count >= lmt) {
return;
}
yield value;
count++;
}
}
export function* genFromStart(start: number, end: number) {
let currentValue = start;
while (currentValue <= end) {
yield currentValue;
}
}