-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathscriptum.js
8434 lines (5597 loc) · 227 KB
/
scriptum.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
__ __ __ __ ___
/__` / ` |__) | |__) | | | |\/|
.__/ \__, | \ | | | \__/ | |
lib: functional base library
Focus on functional idioms, asynchronicity, iterators, arrays, strings, linear
algebra, and regular expressions for effective natural language processing in
Javascript. */
/*█████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
████████████████████████████████████ DEPS █████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
import nodeChild from "node:child_process";
import nodeCrypto from "node:crypto";
import nodeFs from "node:fs";
import nodePath from "node:path";
import nodeStream from "node:stream";
// TODO: immutable.js
/*█████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
███████████████████████████████████ ASPECTS ███████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
// cross-cutting concernes
/*█████████████████████████████████████████████████████████████████████████████
██████████████████████████████████ CONSTANTS ██████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
const $ = Symbol.toStringTag;
const $$ = Symbol("constructor");
export const Err = Error;
export const noop = null;
export const not_found = -1;
/*█████████████████████████████████████████████████████████████████████████████
████████████████████████████████████ TYPES ████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
/* Tag a zero-argument function as a thunk. The idea is to distinguish thunks
for deferred evaluation of expressions from arbitrary zero-argument functions. */
export const Thunk = thunk => ({
[$]: "Thunk",
[$$]: "Thunk",
thunk
});
//█████ Sum Types █████████████████████████████████████████████████████████████*/
/* Define sum types in continuation passing style. Sum types represent values
that can have different shapes and are still type safe.
const None = variant0({
tag: "Option",
cons: "None",
keys: ["none", "some"] // order matters
}),
const Some = variant({
tag: "Option",
cons: "Some",
keys: ["some", "none"] // order matters
});
const sqr = f => f({
some: x => x * x,
none: () => 0
});
const inc = f => f({
done: x => x + 1,
none: () => 0
});
const o = Some(5), p = None;
sqr(o) // yields 25
sqr(p) // yields 0
inc(o) // throws missing key "some" */
const variant0 = ({tag, cons, keys}) => {
const prop = keys[0];
const wrapper = {
[prop]: o => {
for (const k of keys)
if (!(k in o)) throw new Err(`missing key "${k}"`);
const r = o[prop] ();
if (r === undefined || r !== r) throw new Err(r);
else return r;
}
};
wrapper[prop] [$] = tag;
wrapper[prop] [$$] = cons;
return wrapper[prop];
};
const variant = ({tag, cons, keys}) => x => {
const prop = keys[0];
const wrapper = {
[prop]: o => {
for (const k of keys)
if (!(k in o)) throw new Err(`missing key "${k}"`);
const r = o[prop] (x);
if (r === undefined || r !== r) throw new Err(r);
else return r;
}
};
wrapper[prop] [$] = tag;
wrapper[prop] [$$] = cons;
return wrapper[prop];
};
const variant2 = ({tag, cons, keys}) => x => y => {
const prop = keys[0];
const wrapper = {
[prop]: o => {
for (const k of keys)
if (!(k in o)) throw new Err(`missing key "${k}"`);
const r = o[prop] (x);
if (r === undefined || r !== r) throw new Err(r);
else return r;
}
};
wrapper[prop] [$] = tag;
wrapper[prop] [$$] = cons;
return wrapper[prop];
};
const variantn = ({tag, cons, keys}) => (...args) => {
const prop = keys[0];
const wrapper = {
[prop]: o => {
for (const k of keys)
if (!(k in o)) throw new Err(`missing key "${k}"`);
const r = o[prop] (...args);
if (r === undefined || r !== r) throw new Err(r);
else return r;
}
};
wrapper[prop] [$] = tag;
wrapper[prop] [$$] = cons;
return wrapper[prop];
};
/*█████████████████████████████████████████████████████████████████████████████
███████████████████████████ WELL-KNOWN COMBINATORS ████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
export const id = x => x;
export const comp = f => g => x => f(g(x));
export const pipe = g => f => x => f(g(x));
export const compn = (...fs) => fs.reduce((acc, f) => comp(f) (acc), id);
export const pipen = (...fs) => fs.reduce((acc, f) => pipe(f) (acc), id);
export const lift = f => g => x => y => f(g(x)) (g(y));
export const liftl = f => g => x => y => f(g(x) (y));
export const liftr = f => g => x => y => f(x) (g(y));
export const constl = x => y => x;
export const constr = x => y => y;
export const curry = f => x => y => f(x, y);
export const uncurry = f => (x, y) => f(x) (y);
export const partial = (f, ...args) => (...args2) => f(...args, ...args2);
export const flip = f => y => x => f(x) (y);
export const app = f => x => f(x);
export const ppa = x => f => f(x);
export const appr = (f, y) => x => f(x) (y);
// object notation with self-reference like `this`
export const reify = f => f({});
export const appObj = f => o =>
Object.values(o).reduce((acc, v) => acc(v), f);
export const appTuple = f => xs =>
xs.reduce((acc, x) => acc(x), f);
// mimic let bindings
export const _let = (...args) => ({in: f => f(...args)});
// explicit scope
export const scope = f => f();
/* Avoid function call nesting by simulating infix operator use. The combinator
works in two modes:
(x, f, y, g, z) => g(f(x) (y)) (z) // left
(x, f, y, g, z) => g(z) (f(x) (y)) // right
Function composition and applicative computations build a function call tree in
their left argument. Functorial computations build it in their right arrgument. */
const infix = left => (...args) => {
if (args.length === 0) throw new Err("no argument found");
else if (args.length % 2 === 0) throw new Err("invalid number of arguments");
let i = 1, x = args[0];
while (i < args.length) {
if (i === 1) x = args[i++] (x) (args[i++]);
else if (left) x = args[i++] (x) (args[i++]);
else x = args[i++] (args[i++]) (x);
}
return x;
};
export const infixl = infix(true);
export const infixr = infix(false);
// explicit receiver
export const This = t => ({
app: x => This(t(x)),
map: f => This(f(t)),
unref: t,
});
export const _throw = e => {throw e};
export const _try = f => x => ({
catch: handler => {
try {return f(x)}
catch(e) {return handler(x) (e)};
}
});
export const eff = f => x => (f(x), x);
// list effectful expressions and return all results
export const effs = (...exps) => exps;
//█████ Debugging █████████████████████████████████████████████████████████████
export const log = (...args) => {
console.log(...args);
return args[0];
};
export const intro = x =>
Object.prototype.toString.call(x).slice(8, -1);
export const debug = expr => {
debugger;
return expr;
};
export const debugf = f => (...args) => {
debugger;
return f(...args);
};
export const debugIf = p => expr => {
if (p(expr)) debugger;
return expr;
};
export const trace = x => {
console.log(JSON.stringify(x));
return x;
};
/*█████████████████████████████████████████████████████████████████████████████
██████████████████████████████ PATTERN MATCHING ███████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
/* Pattern matching using destructuring assignment and validation with the full
expressive power of the language. Destructuring assignments are wrapped in try-
catch blocks to silently catch mismatches. Please note that this approach also
swallows actual errors in the match process. It's an unavoidable trade-off.
After a match downstream matchers are skipped. The default methods terminates
the chain. You can pass a default to avoid final mismatch.
pattern({foo: [2, 3], bar: 4})
.match(({bat: [x, y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: [,x], bar: [,y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: x, bar: [,y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: [,x], bar: y}) => x % 2 === 1 ? undefined : x + y)
.default(); // throws "mismatch"
pattern({foo: [1, 2], bar: 3})
.match(({bat: [x, y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: [,x], bar: [,y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: x, bar: [,y]}) => x % 2 === 1 ? undefined : x + y)
.match(({foo: [,x], bar: y}) => x % 2 === 1 ? undefined : x + y)
.default(); // yields 5 */
class PatternMatch {
constructor(o) {
this.o = o;
this.r = PatternMatch.mismatch;
this.skip = false;
}
static mismatch = Symbol();
match(f) {
if (this.skip) return this;
try {
const r = f(this.o);
if (r !== undefined && r === r) {
this.r = r;
this.skip = true;
}
}
catch (e) {}
return this;
}
default(x) {
if (!this.skip || this.r === PatternMatch.mismatch) {
if (x !== undefined) return x;
else throw new Err("mismatch");
}
else return this.r;
}
}
const pattern = o => new PatternMatch(o);
/*█████████████████████████████████████████████████████████████████████████████
█████████████████████████████████ MEMOIZATION █████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
/* Memoization of recursive functions with a single argument. You can transform
them into CPS form in order to derive memoized and a non-memoized versions from
them:
const app = f => x => f(x);
const cpsFib = k => {
const rec = k(n => n <= 1 ? n : rec(n - 1) + rec(n - 2));
return rec;
};
const fibMemo = cpsFib(memo)
fib = cpsFib(app);
fib(40); // yields 102334155
fibMemo(40); // yields 102334155 much faster
Or simply rely on reference mutation of the original recursive function with
`let fib = n => {...}; fib = memo(fib)` */
const memoize = f => {
const m = new Map();
return x => {
if (m.has(x)) return m.get(x);
else {
const r = f(x);
m.set(x, r);
return r;
}
};
};
// more general version where the key is derived from the value
const memoize_ = f => g => {
const m = new Map();
return x => {
const k = f(x);
if (m.has(k)) return m.get(k);
else {
const y = g(x), k2 = f(y);
m.set(k2, y);
return y;
}
};
};
/*█████████████████████████████████████████████████████████████████████████████
████████████████████████████████ STACK-SAFETY █████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
//█████ Tail Recursion ████████████████████████████████████████████████████████
// stack-safe tail-recursion using a trampoline
export const Loop = f => x => {
let o = f(x);
while (o?.constructor === Loop.rec) {
switch (o.constructor) {
case Loop.call: {
o = o.f(o.x);
break;
}
case Loop.rec: {
o = f(o.x);
break;
}
default: {
if (o?.[$] === "Thunk") o = o();
else throw new Err("trampoline constructor expected");
}
}
}
if (o?.constructor === Loop.base) return o.x;
else return o;
};
export const Loop2 = f => (x, y) => {
let o = f(x, y);
while (o?.constructor === Loop2.rec) {
switch (o.constructor) {
case Loop2.call: {
o = o.f(o.x, o.y);
break;
}
case Loop2.rec: {
o = f(o.x, o.y);
break;
}
default: {
if (o?.[$] === "Thunk") o = o();
else throw new Err("trampoline constructor expected");
}
}
}
if (o?.constructor === Loop2.base) return o.x;
else return o;
};
export const Loop3 = f => (x, y, z) => {
let o = f(x, y, z);
while (o?.constructor === Loop3.rec) {
switch (o.constructor) {
case Loop3.call: {
o = o.f(o.x, o.y, o.z);
break;
}
case Loop3.rec: {
o = f(o.x, o.y, o.z);
break;
}
default: {
if (o?.[$] === "Thunk") o = o();
else throw new Err("trampoline constructor expected");
}
}
}
if (o?.constructor === Loop3.base) return o.x;
else return o;
};
Loop.call = function call(f, x) {
return {constructor: Loop.call, f, x};
};
Loop.rec = function rec(x) {
return {constructor: Loop.rec, x};
};
Loop.base = function base(x) {
return {constructor: Loop.base, x};
};
Loop2.call = function call(f, x, y) {
return {constructor: Loop2.call, f, x, y};
};
Loop2.rec = function rec(x, y) {
return {constructor: Loop2.rec, x, y};
};
Loop2.base = function base(x) {
return {constructor: Loop2.base, x};
};
Loop3.call = function call(f, x, y, z) {
return {constructor: Loop3.call, f, x, y, z};
};
Loop3.rec = function rec(x, y, z) {
return {constructor: Loop3.rec, x, y, z};
};
Loop3.base = function base(x) {
return {constructor: Loop3.base, x};
};
//█████ Virtual Stack █████████████████████████████████████████████████████████
/* Reify the stack and make even non-tail-recursion stack-safe using a trampoline.
This goes way beyond tail-recursion modulo cons et al.:
const fib_ = n =>
n <= 1 ? n
: fib_(n - 1) + fib_(n - 2);
Transformed into the trampoline version it becomes:
const add = x => y => x + y;
const fib = Stack(n =>
n <= 1
? Stack.base(n)
: Stack.call2(
add,
Stack.rec(n - 1),
Stack.rec(n - 2))); */
export const Stack = f => x => {
const stack = [f(x)];
while (stack.length > 1 || stack[0].constructor !== Stack.base) {
let o = stack[stack.length - 1];
switch (o.constructor) {
case Stack.call:
case Stack.call2: {
o = f(o.x.x); // likely not general enough
stack.push(o);
break;
}
case Stack.base: {
while (stack.length > 1 && stack[stack.length - 1].constructor === Stack.base) {
const p = (stack.pop(), stack.pop());
switch (p.constructor) {
case Stack.call: {
o = Stack.base(p.f(o.x));
stack.push(o);
break;
}
case Stack.call2: {
o = Stack.call(p.f(o.x), p.y);
stack.push(o);
break;
}
default: throw new Err("trampoline constructor expected");
}
}
break;
}
default: {
if (o?.[$] === "Thunk") o = o();
else throw new Err("trampoline constructor expected");
}
}
}
return stack[0].x;
};
export const Stack2 = f => (x, y) => {
const stack = [f(x, y)];
while (stack.length > 1 || stack[0].constructor !== Stack2.base) {
let o = stack[stack.length - 1];
switch (o.constructor) {
case Stack2.call:
case Stack2.call2: {
o = f(o.x.x, o.x.y); // likely not general enough
stack.push(o);
break;
}
case Stack2.base: {
while (stack.length > 1 && stack[stack.length - 1].constructor === Stack2.base) {
const p = (stack.pop(), stack.pop());
switch (p.constructor) {
case Stack2.call: {
o = Stack2.base(p.f(o.x, o.y));
stack.push(o);
break;
}
case Stack2.call2: {
o = Stack2.call(p.f(o.x, o.y), p.y);
stack.push(o);
break;
}
default: throw new Err("trampoline constructor expected");
}
}
break;
}
default: {
if (o?.[$] === "Thunk") o = o();
else throw new Err("trampoline constructor expected");
}
}
}
return stack[0].x;
};
// constructors
Stack.call = function call(f, x) {
return {constructor: Stack.call, f, x};
};
Stack.call2 = function call2(f, x, y) {
return {constructor: Stack.call2, f, x, y};
};
Stack.rec = function rec(x) {
return {constructor: Stack.rec, x};
};
Stack.base = function base(x) {
return {constructor: Stack.base, x};
};
Stack2.call = function call(f, x) {
return {constructor: Stack2.call, f, x};
};
Stack2.call2 = function call2(f, x, y) {
return {constructor: Stack2.call2, f, x, y};
};
Stack2.rec = function rec(x) {
return function rec(y) {
return {constructor: Stack2.rec, x, y};
};
};
Stack2.base = function base(x) {
return {constructor: Stack2.base, x};
};
/*█████████████████████████████████████████████████████████████████████████████
███████████████████████████████ TYPE SIGNATURES ███████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
export const Sign = {};
Sign.get = x => {
if (x === null) return "Nul";
else if (x === undefined) return "Und";
else if (typeof x === "object") {
const tag = Object.prototype.toString.call(x).slice(8, -1);
switch (tag) {
case "Array": return `[${Sign.arr(x)}]`;
case "Boolean": return "Bol{}";
case "Date": return "Dat{}";
case "Map": return `Map<${Sign.map(x)}>`;
case "Number": return "Num{}";
case "Promise": return "Pro{}";
case "RegExp": return "Rex{}";
case "Set": return `Set<${Sign.set(x)}>`;
case "String": return "Str{}";
case "Symbol": return "Sym{}";
case "WeakMap": return `Wap<${Sign.map(x)}>`;
case "WeakRef": return "Ref{}";
case "WeakSet": return `Wet<${Sign.set(x)}>`;
case "Object": {
if (x?.[$]) return `${x[$]}{${Sign.obj(x)}}`;
else {
const name = x?.constructor?.name === "Object"
? "" : x.constructor.name;
return `${name}{${Sign.obj(x)}}`;
}
}
default: throw new Err(`unknown tag "${tag}`);
}
}
else switch (Object.prototype.toString.call(x).slice(8, -1)) {
case "Boolean": return "Boo";
case "BigInt": return "Big";
case "Function": return "=>";
case "NaN": return "NaN";
case "Null": return "Nul";
case "Number": return "Num";
case "String": return "Str";
case "Undefined": return "Und";
default: {
}
}
};
Sign.arr = xs => {
const s = xs.reduce((acc, x) => {
return acc.add(Sign.get(x))
}, new Set());
return Array.from(s).join(",");
};
Sign.set = s => {
const s2 = Array.from(s).reduce((acc, x) => {
return acc.add(Sign.get(x))
}, new Set());
return Array.from(s2).join(",");
};
Sign.map = m => {
const s2 = Array.from(m).reduce((acc, pair) => {
return acc.add(`${Sign.get(pair[0])}:${Sign.get(pair[1])}`);
}, new Set());
return Array.from(s2).join(",");
};
Sign.obj = o => {
const s2 = Object.entries(o).reduce((acc, pair) => {
return acc.add(`${Sign.key(pair[0])}:${Sign.get(pair[1])}`);
}, new Set());
return Array.from(s2).join(",");
};
Sign.key = x => {
if (Object.prototype.toString.call(x).slice(8, -1) === "Symbol")
return "Sym";
else return x;
};
/*█████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
████████████████████████████████████ ARRAY ████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████
███████████████████████████████████████████████████████████████████████████████*/
export const A = {};
A.singleton = x => [x];
A.last = xs => xs.length === 0 ? null : xs[xs.length - 1];
A.push = x => xs => (xs.push(x), xs);
A.pushr = xs => x => (xs.push(x), xs);
A.pushn = ys => xs => (xs.push.apply(xs, ys), xs);
A.pushnr = xs => ys => (xs.push.apply(xs, ys), xs);
A.unshift = x => xs => (xs.unshift(x), xs);
A.unshiftr = xs => x => (xs.unshift(x), xs);
A.unshiftn = ys => xs => (xs.unshift.apply(xs, ys), xs);
A.unshiftnr = xs => ys => (xs.unshift.apply(xs, ys), xs);
A.pop = xs => Pair(xs.length === 0 ? null : xs.pop(), xs);
A.shift = xs => Pair(xs.length === 0 ? null : xs.shift(), xs);
A.cons = x => xs => [x].concat(xs);
A.consr = xs => x => [x].concat(xs);
A.snoc = x => xs => xs.concat([x]);
A.snocr = xs => x => xs.concat([x]);
A.uncons = xs => [xs.length === 0 ? null : xs[0], xs.slice(1)];
A.unsnoc = xs => [
xs.length === 0 ? null : xs[xs.length - 1],
xs.slice(-1)
];
A.init = xs => xs.length === 0 ? null : xs.slice(0, -1);
A.inits = (arr) => {
if (!Array.isArray(arr)) {
throw new TypeError("Input must be an array.");
}
const n = arr.length;
const result = [];
for (let i = n - 1; i >= 1; i--) {
result.push(arr.slice(0, i));
}
return result;
};
A.tail = xs => xs.length === 0 ? null : xs.slice(1);
A.tails = (arr) => {
if (!Array.isArray(arr)) {
throw new TypeError("Input must be an array.");
}
const n = arr.length;
const result = [];
for (let i = 1; i <= n - 1; i++) {