-
Notifications
You must be signed in to change notification settings - Fork 2
/
functional.js
1477 lines (1477 loc) · 50.2 KB
/
functional.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
Either.prototype.alt = Either.prototype["fantasy-land/alt"] = function(container) {
return this.fold({
Left: (_)=>container
,
Right: (_)=>this
});
};
Either.prototype.chain = Either.prototype["fantasy-land/chain"] = function(unaryFunction) {
return this.fold({
Left: (_)=>this
,
Right: (value)=>unaryFunction(value)
});
};
Either.prototype.extract = Either.prototype["fantasy-land/extract"] = function() {
return this.fold({
Left: (_)=>this
,
Right: (value)=>value
});
};
Either.prototype.reduce = Either.prototype["fantasy-land/reduce"] = function(binaryFunction, accumulator) {
return this.fold({
Left: (_)=>accumulator
,
Right: (value)=>binaryFunction(accumulator, value)
});
};
Either.prototype.sequence = function(TypeRepresentation) {
return this.traverse(TypeRepresentation, (x)=>x
);
};
IO.prototype.ap = IO.prototype["fantasy-land/ap"] = function(container) {
return container.map((unaryFunction)=>unaryFunction(this.asyncFunction())
);
};
IO.prototype.run = function() {
return this.asyncFunction();
};
Maybe.prototype.alt = Maybe.prototype["fantasy-land/alt"] = function(container) {
return this.fold({
Nothing: (_)=>container
,
Just: (_)=>this
});
};
Maybe.prototype.reduce = Maybe.prototype["fantasy-land/reduce"] = function(binaryFunction, accumulator) {
return this.fold({
Nothing: (_)=>accumulator
,
Just: (value)=>binaryFunction(accumulator, value)
});
};
Maybe.prototype.sequence = function(TypeRepresentation) {
return this.traverse(TypeRepresentation, (x)=>x
);
};
function _pipe(f, g) {
return function() {
return g.call(this, f.apply(this, arguments));
};
}
function _identity(x) {
return x;
}
function _isFunction(x) {
var type = Object.prototype.toString.call(x);
return type === '[object Function]' || type === '[object AsyncFunction]' || type === '[object GeneratorFunction]' || type === '[object AsyncGeneratorFunction]';
}
const serializeTypeRepresentation = (typeName)=>typeName
;
export const $$debug = Symbol.for("TaskDebug");
export const $$inspect = typeof Deno !== "undefined" ? Deno.customInspect : "inspect";
export const $$returnType = Symbol.for("ReturnType");
export const $$tag = Symbol.for("Tag");
export const $$tagList = Symbol.for("TagList");
export const $$type = Symbol.for("Type");
export const $$value = Symbol.for("Value");
export const $$valueList = Symbol.for("ValueList");
Step.prototype.alt = Step.prototype["fantasy-land/alt"] = function(container) {
return this.fold({
Done: (_)=>container
,
Loop: (_)=>this
});
};
const concat = (x)=>(y)=>x.concat(y)
;
const serializeFunctionForDebug = (asyncFunction)=>asyncFunction.name && asyncFunction.name !== "" ? asyncFunction.name : asyncFunction.toString().length > 25 ? asyncFunction.toString().slice(0, 25).replace(/[\n\r]/g, "").replace(/\s\s*/g, " ") + "[...]" : asyncFunction.toString().replace(/[\n\r]/g, "").replace(/\s\s*/g, " ")
;
Task.prototype.run = async function() {
const maybePromise = this.asyncFunction();
return (maybePromise instanceof Promise ? maybePromise : Promise.resolve(maybePromise)).then((maybeContainer)=>__default.is(maybeContainer) ? maybeContainer : __default.Right(maybeContainer)
, (maybeContainer)=>__default.is(maybeContainer) ? maybeContainer : __default.Left(maybeContainer)
);
};
Task.prototype.toString = Task.prototype[$$inspect] = function() {
return this[$$debug] || `Task("unknown")`;
};
const noColor = globalThis.Deno?.noColor ?? true;
let enabled = !noColor;
function code(open, close) {
return {
open: `\x1b[${open.join(";")}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g")
};
}
function run(str, code1) {
return enabled ? `${code1.open}${str.replace(code1.regexp, code1.open)}${code1.close}` : str;
}
function red(str) {
return run(str, code([
31
], 39));
}
function blue(str) {
return run(str, code([
34
], 39));
}
function brightBlack(str) {
return run(str, code([
90
], 39));
}
function clampAndTruncate(n, max = 255, min = 0) {
return Math.trunc(Math.max(Math.min(n, max), min));
}
const ANSI_PATTERN = new RegExp([
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|"), "g");
const $$decoder = new TextDecoder();
const $$encoder = new TextEncoder();
export const assertIsArray = (value)=>value instanceof Array
;
export const assertIsBoolean = (value)=>value === true || value === false
;
export const assertIsFunction = (value)=>value instanceof Function
;
export const assertIsNull = (value)=>value === null
;
export const assertIsNumber = (value)=>typeof value === "number"
;
export const assertIsObject = (value)=>typeof value === "object" && !(value instanceof Array)
;
export const assertIsRegex = (value)=>value instanceof RegExp
;
export const assertIsString = (value)=>typeof value === "string"
;
export const assertIsUndefined = (value)=>value === undefined
;
export const decodeRaw = $$decoder.decode.bind($$decoder);
export const encodeText = $$encoder.encode.bind($$encoder);
export const log = (message)=>(x)=>console.debug(blue(message), x) || x
;
function XMap(f, xf) {
this.xf = xf;
this.f = f;
}
XMap.prototype['@@transducer/step'] = function(result, input) {
return this.xf['@@transducer/step'](result, this.f(input));
};
const __default4 = Number.isInteger || function _isInteger(n) {
return n << 0 === n;
};
function _arrayFromIterator(iter) {
var list = [];
var next;
while(!(next = iter.next()).done){
list.push(next.value);
}
return list;
}
function _includesWith(pred, x, list) {
var idx = 0;
var len = list.length;
while(idx < len){
if (pred(x, list[idx])) {
return true;
}
idx += 1;
}
return false;
}
function _functionName(f) {
var match = String(f).match(/^function (\w*)/);
return match == null ? '' : match[1];
}
function _objectIs(a, b) {
if (a === b) {
return a !== 0 || 1 / a === 1 / b;
} else {
return a !== a && b !== b;
}
}
const __default1 = typeof Object.is === 'function' ? Object.is : _objectIs;
function _quote(s) {
var escaped = s.replace(/\\/g, '\\\\').replace(/[\b]/g, '\\b').replace(/\f/g, '\\f').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t').replace(/\v/g, '\\v').replace(/\0/g, '\\0');
return '"' + escaped.replace(/"/g, '\\"') + '"';
}
var pad = function pad(n) {
return (n < 10 ? '0' : '') + n;
};
var _toISOString = typeof Date.prototype.toISOString === 'function' ? function _toISOString(d) {
return d.toISOString();
} : function _toISOString(d) {
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + '.' + (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z';
};
function _complement(f) {
return function() {
return !f.apply(this, arguments);
};
}
function _filter(fn, list) {
var idx = 0;
var len = list.length;
var result = [];
while(idx < len){
if (fn(list[idx])) {
result[result.length] = list[idx];
}
idx += 1;
}
return result;
}
function _isObject(x) {
return Object.prototype.toString.call(x) === '[object Object]';
}
function XFilter(f, xf) {
this.xf = xf;
this.f = f;
}
XFilter.prototype['@@transducer/step'] = function(result, input) {
return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
};
function _map(fn, functor) {
var idx = 0;
var len = functor.length;
var result = Array(len);
while(idx < len){
result[idx] = fn(functor[idx]);
idx += 1;
}
return result;
}
function XWrap(fn) {
this.f = fn;
}
XWrap.prototype['@@transducer/init'] = function() {
throw new Error('init not implemented on XWrap');
};
XWrap.prototype['@@transducer/result'] = function(acc) {
return acc;
};
XWrap.prototype['@@transducer/step'] = function(acc, x) {
return this.f(acc, x);
};
function _xwrap(fn) {
return new XWrap(fn);
}
function _arrayReduce(xf, acc, list) {
var idx = 0;
var len = list.length;
while(idx < len){
acc = xf['@@transducer/step'](acc, list[idx]);
if (acc && acc['@@transducer/reduced']) {
acc = acc['@@transducer/value'];
break;
}
idx += 1;
}
return xf['@@transducer/result'](acc);
}
function _iterableReduce(xf, acc, iter) {
var step = iter.next();
while(!step.done){
acc = xf['@@transducer/step'](acc, step.value);
if (acc && acc['@@transducer/reduced']) {
acc = acc['@@transducer/value'];
break;
}
step = iter.next();
}
return xf['@@transducer/result'](acc);
}
var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
function _isTransformer(obj) {
return obj != null && typeof obj['@@transducer/step'] === 'function';
}
var toString1 = Object.prototype.toString;
var hasEnumBug = !({
toString: null
}).propertyIsEnumerable('toString');
var nonEnumerableProps = [
'constructor',
'valueOf',
'isPrototypeOf',
'toString',
'propertyIsEnumerable',
'hasOwnProperty',
'toLocaleString'
];
var hasArgsEnumBug = function() {
'use strict';
return arguments.propertyIsEnumerable('length');
}();
var contains = function contains(list, item) {
var idx = 0;
while(idx < list.length){
if (list[idx] === item) {
return true;
}
idx += 1;
}
return false;
};
function _concat(set1, set2) {
set1 = set1 || [];
set2 = set2 || [];
var idx;
var len1 = set1.length;
var len2 = set2.length;
var result = [];
idx = 0;
while(idx < len1){
result[result.length] = set1[idx];
idx += 1;
}
idx = 0;
while(idx < len2){
result[result.length] = set2[idx];
idx += 1;
}
return result;
}
function _isPlaceholder(a) {
return a != null && typeof a === 'object' && a['@@functional/placeholder'] === true;
}
function _arity(n, fn) {
switch(n){
case 0:
return function() {
return fn.apply(this, arguments);
};
case 1:
return function(a0) {
return fn.apply(this, arguments);
};
case 2:
return function(a0, a1) {
return fn.apply(this, arguments);
};
case 3:
return function(a0, a1, a2) {
return fn.apply(this, arguments);
};
case 4:
return function(a0, a1, a2, a3) {
return fn.apply(this, arguments);
};
case 5:
return function(a0, a1, a2, a3, a4) {
return fn.apply(this, arguments);
};
case 6:
return function(a0, a1, a2, a3, a4, a5) {
return fn.apply(this, arguments);
};
case 7:
return function(a0, a1, a2, a3, a4, a5, a6) {
return fn.apply(this, arguments);
};
case 8:
return function(a0, a1, a2, a3, a4, a5, a6, a7) {
return fn.apply(this, arguments);
};
case 9:
return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {
return fn.apply(this, arguments);
};
case 10:
return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
return fn.apply(this, arguments);
};
default:
throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
}
}
function _isString(x) {
return Object.prototype.toString.call(x) === '[object String]';
}
function _has(prop, obj) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
const __default2 = Array.isArray || function _isArray(val) {
return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
};
const __default3 = {
init: function() {
return this.xf['@@transducer/init']();
},
result: function(result) {
return this.xf['@@transducer/result'](result);
}
};
function _checkForMethod(methodname, fn) {
return function() {
var length = arguments.length;
if (length === 0) {
return fn();
}
var obj = arguments[length - 1];
return __default2(obj) || typeof obj[methodname] !== 'function' ? fn.apply(this, arguments) : obj[methodname].apply(obj, Array.prototype.slice.call(arguments, 0, length - 1));
};
}
const serializeTypeRepresentationBound = function() {
return serializeTypeRepresentation(this[$$type]);
};
function gray(str) {
return brightBlack(str);
}
XMap.prototype['@@transducer/init'] = __default3.init;
XMap.prototype['@@transducer/result'] = __default3.result;
XFilter.prototype['@@transducer/init'] = __default3.init;
XFilter.prototype['@@transducer/result'] = __default3.result;
function _curryN(length, received, fn) {
return function() {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
while(combinedIdx < received.length || argsIdx < arguments.length){
var result;
if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (!_isPlaceholder(result)) {
left -= 1;
}
combinedIdx += 1;
}
return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
};
}
function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0 || _isPlaceholder(a)) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
}
var _isArrayLike = _curry1(function isArrayLike(x) {
if (__default2(x)) {
return true;
}
if (!x) {
return false;
}
if (typeof x !== 'object') {
return false;
}
if (_isString(x)) {
return false;
}
if (x.length === 0) {
return true;
}
if (x.length > 0) {
return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
}
return false;
});
function _dispatchable(methodNames, transducerCreator, fn) {
return function() {
if (arguments.length === 0) {
return fn();
}
var obj = arguments[arguments.length - 1];
if (!__default2(obj)) {
var idx = 0;
while(idx < methodNames.length){
if (typeof obj[methodNames[idx]] === 'function') {
return obj[methodNames[idx]].apply(obj, Array.prototype.slice.call(arguments, 0, -1));
}
idx += 1;
}
if (_isTransformer(obj)) {
var transducer = transducerCreator.apply(null, Array.prototype.slice.call(arguments, 0, -1));
return transducer(obj);
}
}
return fn.apply(this, arguments);
};
}
var _isArguments = function() {
return toString1.call(arguments) === '[object Arguments]' ? function _isArguments(x) {
return toString1.call(x) === '[object Arguments]';
} : function _isArguments(x) {
return _has('callee', x);
};
}();
var keys = typeof Object.keys === 'function' && !hasArgsEnumBug ? _curry1(function keys(obj) {
return Object(obj) !== obj ? [] : Object.keys(obj);
}) : _curry1(function keys(obj) {
if (Object(obj) !== obj) {
return [];
}
var prop, nIdx;
var ks = [];
var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
for(prop in obj){
if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
ks[ks.length] = prop;
}
}
if (hasEnumBug) {
nIdx = nonEnumerableProps.length - 1;
while(nIdx >= 0){
prop = nonEnumerableProps[nIdx];
if (_has(prop, obj) && !contains(ks, prop)) {
ks[ks.length] = prop;
}
nIdx -= 1;
}
}
return ks;
});
var not = _curry1(function not(a) {
return !a;
});
var reverse = _curry1(function reverse(list) {
return _isString(list) ? list.split('').reverse().join('') : Array.prototype.slice.call(list, 0).reverse();
});
var isNil = _curry1(function isNil(x) {
return x == null;
});
var identity = _curry1(_identity);
var type = _curry1(function type(val) {
return val === null ? 'Null' : val === undefined ? 'Undefined' : Object.prototype.toString.call(val).slice(8, -1);
});
function _curry2(fn) {
return function f2(a, b) {
switch(arguments.length){
case 0:
return f2;
case 1:
return _isPlaceholder(a) ? f2 : _curry1(function(_b) {
return fn(a, _b);
});
default:
return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function(_a) {
return fn(_a, b);
}) : _isPlaceholder(b) ? _curry1(function(_b) {
return fn(a, _b);
}) : fn(a, b);
}
};
}
var curryN = _curry2(function curryN(length, fn) {
if (length === 1) {
return _curry1(fn);
}
return _arity(length, _curryN(length, [], fn));
});
var bind = _curry2(function bind(fn, thisObj) {
return _arity(fn.length, function() {
return fn.apply(thisObj, arguments);
});
});
function _methodReduce(xf, acc, obj, methodName) {
return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
}
function _reduce(fn, acc, list) {
if (typeof fn === 'function') {
fn = _xwrap(fn);
}
if (_isArrayLike(list)) {
return _arrayReduce(fn, acc, list);
}
if (typeof list['fantasy-land/reduce'] === 'function') {
return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
}
if (list[symIterator] != null) {
return _iterableReduce(fn, acc, list[symIterator]());
}
if (typeof list.next === 'function') {
return _iterableReduce(fn, acc, list);
}
if (typeof list.reduce === 'function') {
return _methodReduce(fn, acc, list, 'reduce');
}
throw new TypeError('reduce: list must be array or iterable');
}
function _curry3(fn) {
return function f3(a, b, c) {
switch(arguments.length){
case 0:
return f3;
case 1:
return _isPlaceholder(a) ? f3 : _curry2(function(_b, _c) {
return fn(a, _b, _c);
});
case 2:
return _isPlaceholder(a) && _isPlaceholder(b) ? f3 : _isPlaceholder(a) ? _curry2(function(_a, _c) {
return fn(_a, b, _c);
}) : _isPlaceholder(b) ? _curry2(function(_b, _c) {
return fn(a, _b, _c);
}) : _curry1(function(_c) {
return fn(a, b, _c);
});
default:
return _isPlaceholder(a) && _isPlaceholder(b) && _isPlaceholder(c) ? f3 : _isPlaceholder(a) && _isPlaceholder(b) ? _curry2(function(_a, _b) {
return fn(_a, _b, c);
}) : _isPlaceholder(a) && _isPlaceholder(c) ? _curry2(function(_a, _c) {
return fn(_a, b, _c);
}) : _isPlaceholder(b) && _isPlaceholder(c) ? _curry2(function(_b, _c) {
return fn(a, _b, _c);
}) : _isPlaceholder(a) ? _curry1(function(_a) {
return fn(_a, b, c);
}) : _isPlaceholder(b) ? _curry1(function(_b) {
return fn(a, _b, c);
}) : _isPlaceholder(c) ? _curry1(function(_c) {
return fn(a, b, _c);
}) : fn(a, b, c);
}
};
}
var apply = _curry2(function apply(fn, args) {
return fn.apply(this, args);
});
var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, list) {
return Array.prototype.slice.call(list, fromIndex, toIndex);
}));
var tail = _curry1(_checkForMethod('tail', slice(1, Infinity)));
var max = _curry2(function max(a, b) {
return b > a ? b : a;
});
var hasPath = _curry2(function hasPath(_path, obj) {
if (_path.length === 0 || isNil(obj)) {
return false;
}
var val = obj;
var idx = 0;
while(idx < _path.length){
if (!isNil(val) && _has(_path[idx], val)) {
val = val[_path[idx]];
idx += 1;
} else {
return false;
}
}
return true;
});
var has = _curry2(function has(prop, obj) {
return hasPath([
prop
], obj);
});
var zipObj = _curry2(function zipObj(keys1, values) {
var idx = 0;
var len = Math.min(keys1.length, values.length);
var out = {
};
while(idx < len){
out[keys1[idx]] = values[idx];
idx += 1;
}
return out;
});
const factorizeFold = (functionByTag, instanceTag, constructorTagList)=>{
for (const tag of constructorTagList){
if (!functionByTag[tag]) {
throw new TypeError(`Constructors given to fold didn't include: ${tag}`);
}
}
return apply(functionByTag[instanceTag]);
};
const factorizeFoldBound = function(functionByTag) {
return factorizeFold(functionByTag, this[$$tag], this.constructor[$$tagList])(this[$$valueList]);
};
var append = _curry2(function append(el, list) {
return _concat(list, [
el
]);
});
var curry = _curry1(function curry(fn) {
return curryN(fn.length, fn);
});
var _xmap = _curry2(function _xmap(f, xf) {
return new XMap(f, xf);
});
var map = _curry2(_dispatchable([
'fantasy-land/map',
'map'
], _xmap, function map(fn, functor) {
switch(Object.prototype.toString.call(functor)){
case '[object Function]':
return curryN(functor.length, function() {
return fn.call(this, functor.apply(this, arguments));
});
case '[object Object]':
return _reduce(function(acc, key) {
acc[key] = fn(functor[key]);
return acc;
}, {
}, keys(functor));
default:
return _map(fn, functor);
}
}));
var nth = _curry2(function nth(offset, list) {
var idx = offset < 0 ? list.length + offset : offset;
return _isString(list) ? list.charAt(idx) : list[idx];
});
var prop = _curry2(function prop(p, obj) {
if (obj == null) {
return;
}
return __default4(p) ? nth(p, obj) : obj[p];
});
var reduce = _curry3(_reduce);
var _xfilter = _curry2(function _xfilter(f, xf) {
return new XFilter(f, xf);
});
var filter = _curry2(_dispatchable([
'fantasy-land/filter',
'filter'
], _xfilter, function(pred, filterable) {
return _isObject(filterable) ? _reduce(function(acc, key) {
if (pred(filterable[key])) {
acc[key] = filterable[key];
}
return acc;
}, {
}, keys(filterable)) : _filter(pred, filterable);
}));
var reject = _curry2(function reject(pred, filterable) {
return filter(_complement(pred), filterable);
});
var ap = _curry2(function ap(applyF, applyX) {
return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function(x) {
return applyF(x)(applyX(x));
} : _reduce(function(acc, f) {
return _concat(acc, map(f, applyX));
}, [], applyF);
});
var liftN = _curry2(function liftN(arity, fn) {
var lifted = curryN(arity, fn);
return curryN(arity, function() {
return _reduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
});
});
var lift = _curry1(function lift(fn) {
return liftN(fn.length, fn);
});
var complement = lift(not);
function pipe() {
if (arguments.length === 0) {
throw new Error('pipe requires at least one argument');
}
return _arity(arguments[0].length, reduce(_pipe, arguments[0], tail(arguments)));
}
function compose() {
if (arguments.length === 0) {
throw new Error('compose requires at least one argument');
}
return pipe.apply(this, reverse(arguments));
}
var pluck = _curry2(function pluck(p, list) {
return map(prop(p), list);
});
var converge = _curry2(function converge(after, fns) {
return curryN(reduce(max, 0, pluck('length', fns)), function() {
var args = arguments;
var context = this;
return after.apply(context, _map(function(fn) {
return fn.apply(context, args);
}, fns));
});
});
const assertIsUnit = curry((instance, value)=>instance === value || !!value && instance[$$tag] === value[$$tag] && instance.constructor[$$type] === value.constructor[$$type]
);
const assertIsTypeRepresentation = curry((typeName, value)=>value !== undefined && value !== null && typeName === value.constructor[$$type]
);
const assertIsVariant = curry((instance, value)=>!!value && instance[$$tag] === value[$$tag] && instance[$$returnType] === value.constructor[$$type]
);
const serializeConstructorType = curry((typeName, tag)=>`${typeName}.${tag}`
);
const serializeConstructorTypeBound = function() {
return serializeConstructorType(this[$$returnType], this[$$tag]);
};
const factorizeValue = curry((propertyNameList, prototype, propertyValueList, argumentCount)=>{
if (argumentCount !== propertyNameList.length) {
throw new TypeError(`Expected ${propertyNameList.length} arguments, got ${argumentCount}.`);
}
return Object.assign(Object.create(prototype), {
...zipObj(propertyNameList, propertyValueList),
[$$valueList]: propertyValueList
});
});
const factorizeConstructor = (propertyNameList, prototype)=>{
switch(propertyNameList.length){
case 1:
return function(a) {
return factorizeValue(propertyNameList, prototype, [
a
], arguments.length);
};
case 2:
return function(a, b) {
return factorizeValue(propertyNameList, prototype, [
a,
b
], arguments.length);
};
case 3:
return function(a, b, c) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c
], arguments.length);
};
case 4:
return function(a, b, c, d) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d
], arguments.length);
};
case 5:
return function(a, b, c, d, e) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e
], arguments.length);
};
case 6:
return function(a, b, c, d, e, f) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e,
f
], arguments.length);
};
case 7:
return function(a, b, c, d, e, f, g) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e,
f,
g
], arguments.length);
};
case 8:
return function(a, b, c, d, e, f, g, h) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e,
f,
g,
h
], arguments.length);
};
case 9:
return function(a, b, c, d, e, f, g, h, i) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e,
f,
g,
h,
i
], arguments.length);
};
case 10:
return function(a, b, c, d, e, f, g, h, i, j) {
return factorizeValue(propertyNameList, prototype, [
a,
b,
c,
d,
e,
f,
g,
h,
i,
j
], arguments.length);
};
default:
return Object.defineProperty(function() {
return factorizeValue(propertyNameList, prototype, arguments, arguments.length);
}, 'length', {
value: propertyNameList.length
});
}
};
const factorizeConstructorFromObject = (propertyNameList, prototype)=>compose(converge(factorizeValue(propertyNameList, prototype), [
identity,
prop("length")
]), (blueprintObject)=>reduce((accumulator, propertyName)=>{
if (complement(has)(propertyName, blueprintObject)) {
throw new TypeError(`Missing field: ${propertyName}`);
}
return [
...accumulator,
blueprintObject[propertyName]
];
}, [], propertyNameList)
)
;
export const assertIsInstance = curry((T, value)=>value instanceof T
);
export const chainLift = curry((binaryFunction, chainableFunctor, functor)=>chainableFunctor.chain((x)=>functor.map(binaryFunction(x))
)
);
export const chainRec = curry((ternaryFunction, initiator, chainableRecursiveFunctor)=>(chainableRecursiveFunctor.chainRec || chainableRecursiveFunctor["fantasy-land/chainRec"]).call(chainableRecursiveFunctor, ternaryFunction, initiator)
);
export const evert = curry((T, list)=>list.reduce((accumulator, x)=>lift(append)(x, accumulator)
, T.of([]))
);
export const insideOut = evert;
export const runSequentially = (initialChainableFunctor, ...chainableFunctorList)=>reduce((accumulator, chainableFunctor)=>accumulator.chain((_)=>chainableFunctor
)
, initialChainableFunctor, chainableFunctorList)
;
export const safeExtract = curry((message, container)=>container.fold({
Left: (error)=>{
throw new Error(`${message} Error: ${error.hasOwnProperty('raw') ? decodeRaw(error.raw) : `${red(error.message)}\n${gray(error.stack)}`}`);
},
Right: (value)=>value
})
);
export const stream = curry(async (composedFunction, accumulator, iterator)=>{
for await (const data of iterator){
accumulator = composedFunction(accumulator, data);
}
return accumulator;
});
Either.fromNullable = (value)=>!(typeof value !== "undefined") || !value && typeof value === "object" ? Either.Left(value) : Either.Right(value)
;
Either.left = (value)=>Either.Left(value)
;
Either.right = (value)=>Either.Right(value)
;
Either.of = Either.prototype.of = Either.prototype["fantasy-land/of"] = (value)=>Either.Right(value)
;
Either.prototype.ap = Either.prototype["fantasy-land/ap"] = function(container) {
return this.fold({
Left: (_)=>this
,
Right: (value)=>Either.Right.is(container) ? Either.Right(container[$$value](value)) : container
});
};
Either.prototype.extend = Either.prototype["fantasy-land/extend"] = function(unaryFunction) {
return this.fold({
Left: (_)=>this
,
Right: (_)=>Either.of(unaryFunction(this))
});
};
Either.prototype.map = Either.prototype["fantasy-land/map"] = function(unaryFunction) {
return this.fold({
Left: (_)=>this
,
Right: (value)=>Either.of(unaryFunction(value))
});
};
Either.prototype.traverse = Either.prototype["fantasy-land/traverse"] = function(TypeRepresentation, unaryFunction) {
return this.fold({
Left: (value)=>TypeRepresentation.of(Either.Left(value))
,
Right: (value)=>unaryFunction(value).map((x)=>Either.Right(x)
)