-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.js
453 lines (340 loc) · 11.6 KB
/
builtins.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
const ord = char => char.charCodeAt(0);
const chr = String.fromCharCode;
const all = f((...conditions) => conditions.length > 1 ? !(conditions.map(i => Boolean(i)).includes(false)) : all(...conditions[0]));
const any = f((...conditions) => conditions.length > 1 ? conditions.map(i => Boolean(i)).includes(true) : all(...conditions[0]));
const bool = Boolean;
const exec = cmd => (new Function(cmd))(); // Can be blocked by some browsers... because of security issues
const type = obj => { typestr = typeof(obj); return eval(typestr[0].toUpperCase() + typestr.slice(1)); };
const abs = Math.abs;
const __all__ = arr => arr.map(f => exec('' + f));
const map = (func, iterable) => [...iter(iterable)].map(func);
const filter = (func, iterable) => [...iter(iterable)].filter(func);
//*_______________
function isiterable(struct) {
try {
for (let i of struct);
return true;
} catch (TypeError) {
return false;
}
}
function stringify(obj) {}
function map2obj() {}
function f(func) {
function wrapper(...args) {
if (args.length === 1)
return func(...args[0]);
}
return wrapper;
}
function new_err(name, extension = Error, as_func = false) {
class Exception extends extension {
constructor(msg) {
super(msg);
this.name = String(name);
}
}
return !as_func ? Exception : (msg => new Exception(msg));
}
//*___________________
//* Errors:
const Exception = new_arr('Exception');
const AssertionError = new_err('AssertionError');
const ValueError = new_err('ValueError');
const StopIteration = new_err('StopIteration');
const assert = (condition, errmsg) => !condition ? new AssertionError(errmsg) : undefined;
let r = String.raw;
const len = function(collection) {
if (isiterable(collection)) {
let ln = collection.length;
if (!ln) {
let count = 0;
for (let i of collection) count++;
return count;
}
return ln;
}
let count = 0;
for (let i in collection)
count++;
return count;
};
const range = function(stop$start, stop = null, step = 1) {
let arr = [];
let con = (stop == null);
for (let i = con ? 0 : stop$start; i < (con ? stop$start : stop); arr.push(i), i += step);
return arr;
};
const dir = function(obj) {
//* pass as type(obj) to get its attributes
let arr = [];
for (let i in obj)
arr.push(i);
if (arr.length)
return arr;
};
const iter = function*(iterable) {
if (typeof(iterable) === "object") {
for (let i in iterable) yield i;
} else {
let _iterable = iterable.__iter__ ? iterable.__iter__() : iterable;
for (let i of _iterable) yield i;
}
};
const next = function(iterobj) {
if (iterobj.__next__) return iterobj.__next__();
if (!iterobj.next) return TypeError(`'${typeof iterobj}' object is not an iterator!`);
while (true) {
if (iterobj.next().done)
return new StopIteration('');
return iterobj.next().value;
}
};
// Error Prones____________________________________
const max = function(...iterables) {
if (iterables.length === 1)
return max(...iterables[0]);
let elem = (iterables[0]);
if (type(elem) === Number)
return Math.max(...iterables);
else if (isiterable(elem) && len(elem) > -1)
return Math.max(...map(i => len(i), iterables));
};
const min = function(...iterables) {
let arr = [...iterables];
for (let i = 1; i < iterables.length; i++)
arr.splice(arr.indexOf(max(...iterables)));
return [arr, arr[0]];
};
//max = Math.max ;;; min = Mah.min
//____________________________________________________
const zip = function*(...iterables) {
if (iterables.length === 1) {
for (let i of iterables[0]) yield i;
}
let smallest_len = Math.min(...map(i => len(i), iterables));
let iter_items = [];
for (let i of iterables)
iterables[i] = [...iterables[i]]; //Make every iterable an array
for (let i = 0; i < smallest_len; i++) {
for (let iterable of iterables)
iter_items.push(iterable[i]);
yield iter_items;
iter_items = [];
}
};
const enumerate = iterable => zip(range(iterable.length), iterable);
// enumerate = iterable => iter([...iter(iterable)].map((i, n) => [n].concat(i)));
const reversed = iterable => zip([...iterable].reverse());
const sum = function(iterable) {
let n = 0;
for (let i of iterable)
n += i;
return n;
};
const print = function(...vals) {
//* Keyword stuff!
let sep = ' ';
let end = '\n';
let file = console;
let last = vals[vals.length - 1];
if (
typeof(last) === 'object' &&
len(last) <= 3 &&
any(['sep', 'end', 'file'].map((i) => i in last))
) {
let props = vals[vals.length - 1];
sep = props.sep || ' ';
end = props.end || '\n';
file = props.file || console;
vals.pop();
}
//* Stringification
for (let i in range(len(vals))) {
try {
vals[i] = vals[i].__repr__();
} catch (TypeError) {
let strobj = JSON.stringify(vals[i]);
vals[i] = len(strobj) > 2 ? strobj : JSON.constructor(vals[i]);
}
}
let str = [...vals].join(sep) + end;
return dir(file).includes('log') ? file.log(str) : file.write(str);
};
//* Built-in Data Structs_____________________
class list extends Array {
constructor(iterable = []) {
super(...iterable);
this.arr = iterable ? [...iterable] : [];
this.length = this.arr.length;
}
toString() { return JSON.stringify(this.arr); }
__repr__() { this.toString(); }
count(obj) {
let occurance = 0;
for (let i of this.arr)
obj === i ? occurance++ : null;
return occurance;
}
clear() { this.arr = []; }
copy() { return this.arr.copyWithin(); }
append(obj) { this.arr.push(obj); }
extend(iterable) { this.arr = [...this.arr, ...iterable]; }
insert(index, obj) { this.arr.splice(index, 0, obj); }
pop(index = 0) { return this.arr.splice(index, 1)[0]; }
remove(obj) { this.pop(this.arr.indexOf(obj)); }
index(obj) { return this.arr.indexOf(obj); }
reverse() { return this.arr.reverse(); }
sort() { this.arr.sort(); }
}
class dict extends Object {
constructor(iterable) {
super(iterable);
}
items() {
let arr = [];
for (let k in this) arr.push([k, this[k]]);
return arr;
}
pop() {}
popitem() {}
setdefault() {}
update() {}
copy() {}
fromkeys() {}
get(val, alter) { return (this[val] || alter); }
clear() { this.prototype = {}; }
keys() { return Object.keys(this); }
values() { return Object.values(this); }
__repr__() { return JSON.stringify(this); }
__str__() { return this.__repr__(); }
toString() { return this.__repr__(); }
}
class set extends Set {
constructor(iterable) { super(iterable); }
__repr__() {}
__str__() {}
add() {}
clear() {}
copy() {}
difference() {}
difference_update() {}
discard() {}
intersection() {}
intersection_update() {}
isdisjoint() {}
issubset() {}
issuperset() {}
pop() {}
remove() {}
symmetric_difference() {}
symmetric_difference_update() {}
union() {}
update() {}
}
class int extends Number {
constructor(iterable, base = 10) { super(iterable); }
__repr__() {}
__str__() {}
as_integer_ratio() {}
bit_length() {}
conjugate() {}
denominator() {}
from_bytes() {}
imag() {}
numerator() {}
real() {}
to_bytes() {}
}
class float extends Number {
constructor(iterable) { super(iterable); }
__repr__() {}
__str__() {}
as_integer_ratio() {}
conjugate() {}
fromhex() {}
hex() {}
imag() {}
is_integer() {}
real() {}
}
//* Customise toString() method to override console.log behaviour
class str extends String {
constructor(obj) {
let func = obj.__str__ || JSON.stringify;
this.strs = (x = func(obj)).length > 2 ? x : String(obj);
this.sup = super(obj);
}
join(iterable) { return [...iter(iterable)].join(this.strs); }
startswith(chars) { return super.startsWith(chars); }
endswith(chars) { return super.endsWith(chars); }
__repr__() { return this.sup; }
strip() { return super.trim(); }
lstrip() { return super.trimLeft(); }
rstrip() { return super.trimRight(); }
capitalize() { return this[0].toUppercase() + super.slice(1).toLowerCase(); }
title() { return this.capitalize(); }
lower() { return super.toLowerCase(); }
upper() { return super.toUpperCase(); }
count(char, start = 0, end = -1) { return filter(i => i == char, super.slice(start, end)).length; }
center(width, fillchar = ' ') { return super.length > width ? fillchar.repeat(width) + this.strs + fillchar.repeat(width) : this.strs; }
ljust(width, fillchar = ' ') { return super.length > width ? fillchar.repeat(width) + this.strs : this.strs; }
rjust(width, fillchar = ' ') { return super.length > width ? this.strs + fillchar.repeat(width) : this.strs; }
find(sub, start = 0, end = -1) { return super.slice(start, end + 1).indexOf(sub); }
rfind(sub, start = 0, end = -1) { return super.slice(start, end + 1).lastIndexOf(sub); }
index(sub, start = 0, end = -1) { return (ind = this.find(sub, start, end)) !== -1 ? ind : ValueError('substring not found'); }
rindex(sub, start = 0, end = -1) { return (ind = this.rfind(sub, start, end)) !== -1 ? ind : ValueError('substring not found'); }
isascii() {}
isprintable() {}
isidentifier() {}
isdigit() {}
isdecimal() {}
isnumeric() {}
isalpha() { return all(...map(i => range(65, 123).includes(ord(i)), this.strs)); }
isalnum() { return all(this.isalpha(), this.isdecimal(), this.isdigit(), this.isnumeric()); }
isspace() { return all(...map((i => ['\n', '\t', ' '].includes(i)), this.strs)); }
islower() { return this.strs === this.lower(); }
isupper() { return this.strs === this.upper(); }
istitle() { return this.strs === this.title(); }
partition(sep) {
return !super.includes(sep) ? [this.strs, '', ''] : [super.slice(0, this.index(sep)), sep, super.slice(super.lastIndexOf(sep), -1)];
}
rpartition(sep) {
return !super.includes(sep) ? ['', '', this.strs] : [super.slice(0, super.lastIndexOf(sep)), sep, super.slice(super.index(sep), -1)];
}
replace(old, New, count = -1) {
return (count <= -1 || this.count(old) === count) ? super.replaceAll(old, New) :
eval(`super${`.replace(${old},${New})`.repeat(count)}`);
}
//* Yet to work on__________________
swapcase() {
let s = '';
let inds = range(65, 123);
let f = c => inds.includes(ord(c));
for (let i of this.strs) {
if (all(...map(f, this.strs)))
s += i === i.toUpperCase() ? i.toLowerCase() : i.toUpperCase();
else s += i;
}
return s;
}
zfill(width) {
if (width <= super.length)
return this.strs;
let has_sign = this.strs[0] === '+' || this.strs[0] === '-' ? true : false;
return has_sign ? this.strs[0] + '0'.repeat(width) + this.strs.slice(1) : '0'.repeat(width) + this.strs;
}
casefold() { }
encode() { }
expandtabs() { }
split(sep = ' ', maxsplit = -1) {
if (maxsplit < 0)
return super.split(sep);
}
rsplit(sep = null, maxsplit = -1) { }
splitlines(keepends = null) { }
maketrans(x, y = null, z = null) { }
translate(table) { }
format() { }
format_map(object) { }
}