UNPKG

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