UNPKG

17.4 kBJavaScriptView Raw
1'use strict';
2
3function IDENTITY(v) {
4 return v;
5}
6function COMPARE(a, b) {
7 return a < b ? -1 : (a > b ? 1 : 0);
8}
9
10function fromRange(v, V, dv = 1) {
11 var n = (V - v) / dv, a = [];
12 for (var i = 0; i < n; ++i, v += dv)
13 a.push(v);
14 return a;
15}
16function index(x, i) {
17 var X = x.length;
18 return i >= 0 ? Math.min(i, X) : Math.max(X + i, 0);
19}
20function get$1(x, i) {
21 return x[index(x, i)];
22}
23function getAll$1(x, is) {
24 return is.map(i => get$1(x, i));
25}
26function last(x, vd) {
27 return x.length > 0 ? x[x.length - 1] : vd;
28}
29function subsequences(x, n = -1) {
30 return [...isubsequences(x, n)];
31}
32function* isubsequences(x, n = -1) {
33 var X = x.length;
34 if (n > X)
35 return;
36 if (n === X) {
37 yield x;
38 return;
39 }
40 if (n === 0 || X === 0) {
41 yield [];
42 return;
43 }
44 var y = x.slice(0, -1);
45 yield* isubsequences(y, n);
46 for (var s of isubsequences(y, n - 1)) {
47 s.push(x[X - 1]);
48 yield s;
49 }
50}
51function randomValue(x, fr = Math.random) {
52 var i = Math.floor(fr() * x.length);
53 return x[i];
54}
55function randomSubsequence(x, n = -1, fr = Math.random) {
56 var X = x.length;
57 if (n > X)
58 return null;
59 if (n >= 0)
60 return randomSubsequenceFixed(x, n, fr);
61 else
62 return randomSubsequenceAll(x, fr);
63}
64function randomSubsequenceFixed(x, n, fr) {
65 var is = fromRange(0, x.length);
66 randomPermutation$(is, n, fr).sort();
67 return getAll$1(x, is);
68}
69function randomSubsequenceAll(x, fr) {
70 var a = [];
71 for (var v of x)
72 if (fr() < 0.5)
73 a.push(v);
74 return a;
75}
76function randomPermutation$(x, n = -1, fr = Math.random) {
77 var X = x.length;
78 if (n > X)
79 return x;
80 var n = n >= 0 ? n : Math.floor((X + 1) * fr());
81 for (var i = 0; i < n; ++i) {
82 var j = i + Math.floor((X - i) * fr());
83 var t = x[i];
84 x[i] = x[j];
85 x[j] = t;
86 }
87 x.length = n;
88 return x;
89}
90function some$1(x, ft = null) {
91 if (ft)
92 return x.some(ft);
93 else
94 return someBoolean(x);
95}
96function someBoolean(x) {
97 for (var i = 0, I = x.length; i < I; ++i)
98 if (x[i])
99 return true;
100 return false;
101}
102
103function is(v) {
104 return v instanceof Map;
105}
106function keys(x) {
107 return x.keys();
108}
109function values(x) {
110 return x.values();
111}
112function entries(x) {
113 return x.entries();
114}
115function from(x) {
116 return new Map(x);
117}
118function from$(x) {
119 return x instanceof Map ? x : new Map(x);
120}
121function fromLists(x) {
122 var [ks, vs] = x;
123 var iv = vs[Symbol.iterator]();
124 var a = new Map();
125 for (var k of ks)
126 a.set(k, iv.next().value);
127 return a;
128}
129function fromKeys(x, fm = null) {
130 var fm = fm || IDENTITY;
131 var a = new Map(), i = -1;
132 for (var k of x)
133 a.set(k, fm(k, ++i, x));
134 return a;
135}
136function fromValues(x, fm = null) {
137 var fm = fm || IDENTITY;
138 var a = new Map(), i = -1;
139 for (var v of x)
140 a.set(fm(v, ++i, x), v);
141 return a;
142}
143function compare(x, y, fc = null, fm = null) {
144 var fc = fc || COMPARE;
145 var fm = fm || IDENTITY;
146 var ks = unionKeys(x, y);
147 for (var k of ks) {
148 if (!x.has(k))
149 return -1;
150 if (!y.has(k))
151 return 1;
152 var vx = fm(x.get(k), k, x);
153 var vy = fm(y.get(k), k, y);
154 var c = fc(vx, vy);
155 if (c !== 0)
156 return c;
157 }
158 return 0;
159}
160function isEqual(x, y, fc = null, fm = null) {
161 return x.size === y.size && compare(x, y, fc, fm) === 0;
162}
163function size(x) {
164 return x.size;
165}
166function isEmpty(x) {
167 return x.size === 0;
168}
169function get(x, k) {
170 return x.get(k);
171}
172function getAll(x, ks) {
173 return ks.map(k => x.get(k));
174}
175function getPath(x, p) {
176 for (var k of p)
177 x = is(x) ? x.get(k) : undefined;
178 return x;
179}
180function hasPath(x, p) {
181 for (var k of p) {
182 if (!is(x))
183 return false;
184 x = x.get(k);
185 }
186 return true;
187}
188function set(x, k, v) {
189 return new Map(x).set(k, v);
190}
191function set$(x, k, v) {
192 return x.set(k, v);
193}
194function setPath$(x, p, v) {
195 var y = getPath(x, p.slice(0, -1));
196 if (is(y))
197 y.set(last(p), v);
198 return x;
199}
200function swap(x, k, l) {
201 return swap$(new Map(x), k, l);
202}
203function swap$(x, k, l) {
204 var t = x.get(k);
205 x.set(k, x.get(l));
206 x.set(l, t);
207 return x;
208}
209function remove(x, k) {
210 return remove$(new Map(x), k);
211}
212function remove$(x, k) {
213 x.delete(k);
214 return x;
215}
216function removePath$(x, p) {
217 var y = getPath(x, p.slice(0, -1));
218 if (is(y))
219 y.delete(last(p));
220 return x;
221}
222function count(x, ft) {
223 var a = 0;
224 for (var [k, v] of x)
225 if (ft(v, k, x))
226 ++a;
227 return a;
228}
229function countAs(x, fm = null) {
230 var fm = fm || IDENTITY;
231 var a = new Map();
232 for (var [k, v] of x) {
233 var w = fm(v, k, x);
234 var n = a.get(w) || 0;
235 a.set(w, n + 1);
236 }
237 return a;
238}
239function min(x, fc = null, fm = null) {
240 return rangeEntries(x, fc, fm)[0][1];
241}
242function minEntry(x, fc = null, fm = null) {
243 return rangeEntries(x, fc, fm)[0];
244}
245function max(x, fc = null, fm = null) {
246 return rangeEntries(x, fc, fm)[1][1];
247}
248function maxEntry(x, fc = null, fm = null) {
249 return rangeEntries(x, fc, fm)[1];
250}
251function range(x, fc = null, fm = null) {
252 var [a, b] = rangeEntries(x, fc, fm);
253 return [a[1], b[1]];
254}
255function rangeEntries(x, fc = null, fm = null) {
256 var fc = fc || COMPARE;
257 var fm = fm || IDENTITY;
258 var mk, mu, mv;
259 var nk, nu, nv;
260 var i = 0;
261 for (var [k, u] of x) {
262 var v = fm(u, k, x);
263 if (i === 0 || fc(v, mv) < 0) {
264 mk = k;
265 mu = u;
266 mv = v;
267 }
268 if (i === 0 || fc(v, nv) > 0) {
269 nk = k;
270 nu = u;
271 nv = v;
272 }
273 ++i;
274 }
275 return [[mk, mu], [nk, nu]];
276}
277function head(x, ed = []) {
278 for (var e of x)
279 return e;
280 return ed;
281}
282function tail(x) {
283 return drop(x, 1);
284}
285function take(x, n = 1) {
286 var a = new Map(), i = -1;
287 for (var [k, v] of x) {
288 if (++i >= n)
289 break;
290 a.set(k, v);
291 }
292 return a;
293}
294function take$(x, n = 1) {
295 var i = -1;
296 for (var k of x.keys())
297 if (++i >= n)
298 x.delete(k);
299 return x;
300}
301function drop(x, n = 1) {
302 var a = new Map(), i = -1;
303 for (var [k, v] of x)
304 if (++i >= n)
305 a.set(k, v);
306 return a;
307}
308function drop$(x, n = 1) {
309 var i = -1;
310 for (var k of x.keys()) {
311 if (++i >= n)
312 break;
313 x.delete(k);
314 }
315 return x;
316}
317function* subsets(x, n = -1) {
318 for (var ks of subsequences([...x.keys()], n))
319 yield filterAt(x, ks);
320}
321function randomKey(x, fr = Math.random) {
322 return randomValue([...x.keys()], fr);
323}
324function randomEntry(x, fr = Math.random) {
325 return randomValue([...x], fr);
326}
327function randomSubset(x, n = -1, fr = Math.random) {
328 var ks = randomSubsequence([...x.keys()], n, fr);
329 return filterAt(x, ks);
330}
331function has(x, k) {
332 return x.has(k);
333}
334function hasValue(x, v, fc = null, fm = null) {
335 return searchValue(x, v, fc, fm) !== undefined;
336}
337function hasEntry(x, e, fc = null, fm = null) {
338 var fc = fc || COMPARE;
339 var fm = fm || IDENTITY;
340 var [k, v] = e;
341 return x.has(k) && fc(fm(x.get(k), k, x), v) === 0;
342}
343function hasSubset(x, y, fc = null, fm = null) {
344 var fc = fc || COMPARE;
345 var fm = fm || IDENTITY;
346 for (var [k, v] of y) {
347 if (!x.has(k))
348 return false;
349 var wx = fm(x.get(k), k, x);
350 var wy = fm(v, k, y);
351 if (fc(wx, wy) !== 0)
352 return false;
353 }
354 return true;
355}
356function find(x, ft) {
357 for (var [k, v] of x)
358 if (ft(v, k, x))
359 return v;
360}
361function findAll(x, ft) {
362 var a = [];
363 for (var [k, v] of x)
364 if (ft(v, k, x))
365 a.push(v);
366 return a;
367}
368function search(x, ft) {
369 for (var [k, v] of x)
370 if (ft(v, k, x))
371 return k;
372}
373function searchAll(x, ft) {
374 var a = [];
375 for (var [k, v] of x)
376 if (ft(v, k, x))
377 a.push(k);
378 return a;
379}
380function searchValue(x, v, fc = null, fm = null) {
381 var fc = fc || COMPARE;
382 var fm = fm || IDENTITY;
383 var w = fm(v, null, null);
384 for (var [k, u] of x) {
385 var wx = fm(u, k, x);
386 if (fc(wx, w) === 0)
387 return k;
388 }
389}
390function searchValueAll(x, v, fc = null, fm = null) {
391 var fc = fc || COMPARE;
392 var fm = fm || IDENTITY;
393 var w = fm(v, null, null), a = [];
394 for (var [k, u] of x) {
395 var wx = fm(u, k, x);
396 if (fc(wx, w) === 0)
397 a.push(k);
398 }
399 return a;
400}
401function forEach(x, fp) {
402 for (var [k, v] of x)
403 fp(v, k, x);
404}
405function some(x, ft) {
406 for (var [k, v] of x)
407 if (ft(v, k, x))
408 return true;
409 return false;
410}
411function every(x, ft) {
412 for (var [k, v] of x)
413 if (!ft(v, k, x))
414 return false;
415 return true;
416}
417function map(x, fm) {
418 var a = new Map();
419 for (var [k, v] of x)
420 a.set(k, fm(v, k, x));
421 return a;
422}
423function map$(x, fm) {
424 for (var [k, v] of x)
425 x.set(k, fm(v, k, x));
426 return x;
427}
428function reduce(x, fr, acc) {
429 var init = arguments.length <= 2;
430 for (var [k, v] of x) {
431 if (init) {
432 acc = v;
433 init = false;
434 }
435 else
436 acc = fr(acc, v, k, x);
437 }
438 return acc;
439}
440function filter(x, ft) {
441 var a = new Map();
442 for (var [k, v] of x)
443 if (ft(v, k, x))
444 a.set(k, v);
445 return a;
446}
447function filter$(x, ft) {
448 for (var [k, v] of x)
449 if (!ft(v, k, x))
450 x.delete(k);
451 return x;
452}
453function filterAt(x, ks) {
454 var a = new Map();
455 for (var k of ks)
456 a.set(k, x.get(k));
457 return a;
458}
459function filterAt$(x, ks) {
460 for (var k of x.keys())
461 if (!ks.includes(k))
462 x.delete(k);
463 return x;
464}
465function reject(x, ft) {
466 var a = new Map();
467 for (var [k, v] of x)
468 if (!ft(v, k, x))
469 a.set(k, v);
470 return a;
471}
472function reject$(x, ft) {
473 for (var [k, v] of x)
474 if (ft(v, k, x))
475 x.delete(k);
476 return x;
477}
478function rejectAt(x, ks) {
479 return rejectAt$(new Map(x), ks);
480}
481function rejectAt$(x, ks) {
482 for (var k of ks)
483 x.delete(k);
484 return x;
485}
486function flat(x, n = -1, fm = null, ft = null) {
487 var fm = fm || IDENTITY;
488 var ft = ft || is;
489 return flatTo$(new Map(), x, n, fm, ft);
490}
491function flatTo$(a, x, n, fm, ft) {
492 for (var [k, v] of x) {
493 var w = fm(v, k, x);
494 if (n !== 0 && ft(w, k, x))
495 flatTo$(a, w, n - 1, fm, ft);
496 else
497 a.set(k, w);
498 }
499 return a;
500}
501function flatMap(x, fm = null, ft = null) {
502 var fm = fm || IDENTITY;
503 var ft = ft || is;
504 var a = new Map();
505 for (var [k, v] of x) {
506 var w = fm(v, k, x);
507 if (ft(w, k, x))
508 concat$(a, w);
509 else
510 a.set(k, w);
511 }
512 return a;
513}
514function zip(xs, fm = null, fe = null, vd) {
515 var fm = fm || IDENTITY;
516 var fe = fe || some$1;
517 var ks = unionKeys(...xs), a = new Map();
518 for (var k of ks) {
519 var ds = xs.map(x => !x.has(k));
520 if (fe(ds))
521 break;
522 var vs = xs.map(x => !x.has(k) ? vd : x.get(k));
523 a.set(k, fm(vs, k, null));
524 }
525 return a;
526}
527function partition(x, ft) {
528 var t = new Map();
529 var f = new Map();
530 for (var [k, v] of x) {
531 if (ft(v, k, x))
532 t.set(k, v);
533 else
534 f.set(k, v);
535 }
536 return [t, f];
537}
538function partitionAs(x, fm) {
539 var fm = fm || IDENTITY;
540 var a = new Map();
541 for (var [k, v] of x) {
542 var w = fm(v, k, x);
543 if (!a.has(w))
544 a.set(w, new Map());
545 a.get(w).set(k, v);
546 }
547 return a;
548}
549function chunk(x, n = 1, s = n) {
550 var ks = [...x.keys()], a = [];
551 for (var i = 0, I = ks.length; i < I; i += s)
552 a.push(filterAt(x, ks.slice(i, i + n)));
553 return a;
554}
555function concat(...xs) {
556 return concat$(new Map(), ...xs);
557}
558function concat$(x, ...ys) {
559 for (var y of ys) {
560 for (var [k, v] of y)
561 x.set(k, v);
562 }
563 return x;
564}
565function join(x, sep = ",", asc = "=") {
566 var a = "";
567 for (var [k, v] of x)
568 a += k + asc + v + sep;
569 return a.slice(0, -sep.length);
570}
571function isDisjoint(x, y) {
572 for (var [k] of y)
573 if (x.has(k))
574 return false;
575 return true;
576}
577function unionKeys(...xs) {
578 var a = new Set();
579 for (var x of xs) {
580 for (var [k] of x)
581 a.add(k);
582 }
583 return a;
584}
585function union(x, y, fc = null) {
586 return union$(new Map(x), y, fc);
587}
588function union$(x, y, fc = null) {
589 var fc = fc || IDENTITY;
590 for (var [k, v] of y) {
591 if (!x.has(k))
592 x.set(k, v);
593 else
594 x.set(k, fc(x.get(k), v));
595 }
596 return x;
597}
598function intersectionKeys(...xs) {
599 var a = new Set();
600 if (xs.length === 0)
601 return a;
602 var x = xs[0], ys = xs.slice(1);
603 LOOPX: for (var k of x.keys()) {
604 for (var y of ys)
605 if (!y.has(k))
606 continue LOOPX;
607 a.add(k);
608 }
609 return a;
610}
611function intersection(x, y, fc = null) {
612 var fc = fc || IDENTITY;
613 var a = new Map();
614 for (var [k, v] of y)
615 if (x.has(k))
616 a.set(k, fc(x.get(k), v));
617 return a;
618}
619function intersection$(x, y, fc = null) {
620 var fc = fc || IDENTITY, ks = [];
621 for (var [k, u] of [...x]) {
622 if (!y.has(k))
623 ks.push(k);
624 x.set(k, fc(u, y.get(k)));
625 }
626 return rejectAt$(x, ks);
627}
628function difference(x, y) {
629 return difference$(new Map(x), y);
630}
631function difference$(x, y) {
632 for (var [k] of y)
633 x.delete(k);
634 return x;
635}
636function symmetricDifference(x, y) {
637 return symmetricDifference$(new Map(x), y);
638}
639function symmetricDifference$(x, y) {
640 for (var [k, v] of y) {
641 if (x.has(k))
642 x.delete(k);
643 else
644 x.set(k, v);
645 }
646 return x;
647}
648function* cartesianProduct(xs, fm = null) {
649 var fm = fm || IDENTITY;
650 var XS = xs.length;
651 var kx = xs.map(x => [...x.keys()]);
652 var ls = kx.map(ks => ks.length);
653 var is = kx.map(ks => 0);
654 for (var j = 0;; ++j) {
655 var a = new Map();
656 for (var n = 0; n < XS; ++n) {
657 var i = is[n], x = xs[n];
658 var ks = kx[n], k = ks[i];
659 a.set(k, x.get(k));
660 }
661 yield fm(a, j, null);
662 for (var r = XS - 1; r >= 0; --r) {
663 if (++is[r] < ls[r])
664 break;
665 is[r] = 0;
666 }
667 if (r < 0)
668 break;
669 }
670}
671
672exports.cartesianProduct = cartesianProduct;
673exports.chunk = chunk;
674exports.compare = compare;
675exports.concat = concat;
676exports.concat$ = concat$;
677exports.count = count;
678exports.countAs = countAs;
679exports.difference = difference;
680exports.difference$ = difference$;
681exports.drop = drop;
682exports.drop$ = drop$;
683exports.entries = entries;
684exports.entry = randomEntry;
685exports.every = every;
686exports.filter = filter;
687exports.filter$ = filter$;
688exports.filterAt = filterAt;
689exports.filterAt$ = filterAt$;
690exports.find = find;
691exports.findAll = findAll;
692exports.flat = flat;
693exports.flatMap = flatMap;
694exports.forEach = forEach;
695exports.from = from;
696exports.from$ = from$;
697exports.fromEntries = from;
698exports.fromEntries$ = from$;
699exports.fromKeys = fromKeys;
700exports.fromLists = fromLists;
701exports.fromValues = fromValues;
702exports.get = get;
703exports.getAll = getAll;
704exports.getPath = getPath;
705exports.has = has;
706exports.hasEntry = hasEntry;
707exports.hasKey = has;
708exports.hasPath = hasPath;
709exports.hasSubset = hasSubset;
710exports.hasValue = hasValue;
711exports.head = head;
712exports.intersection = intersection;
713exports.intersection$ = intersection$;
714exports.intersectionKeys = intersectionKeys;
715exports.is = is;
716exports.isDisjoint = isDisjoint;
717exports.isEmpty = isEmpty;
718exports.isEqual = isEqual;
719exports.join = join;
720exports.key = randomKey;
721exports.keys = keys;
722exports.length = size;
723exports.map = map;
724exports.map$ = map$;
725exports.max = max;
726exports.maxEntry = maxEntry;
727exports.min = min;
728exports.minEntry = minEntry;
729exports.partition = partition;
730exports.partitionAs = partitionAs;
731exports.randomEntry = randomEntry;
732exports.randomKey = randomKey;
733exports.randomSubset = randomSubset;
734exports.range = range;
735exports.rangeEntries = rangeEntries;
736exports.reduce = reduce;
737exports.reject = reject;
738exports.reject$ = reject$;
739exports.rejectAt = rejectAt;
740exports.rejectAt$ = rejectAt$;
741exports.remove = remove;
742exports.remove$ = remove$;
743exports.removePath$ = removePath$;
744exports.search = search;
745exports.searchAll = searchAll;
746exports.searchValue = searchValue;
747exports.searchValueAll = searchValueAll;
748exports.set = set;
749exports.set$ = set$;
750exports.setPath$ = setPath$;
751exports.size = size;
752exports.some = some;
753exports.subset = randomSubset;
754exports.subsets = subsets;
755exports.swap = swap;
756exports.swap$ = swap$;
757exports.symmetricDifference = symmetricDifference;
758exports.symmetricDifference$ = symmetricDifference$;
759exports.tail = tail;
760exports.take = take;
761exports.take$ = take$;
762exports.union = union;
763exports.union$ = union$;
764exports.unionKeys = unionKeys;
765exports.values = values;
766exports.zip = zip;