SHARE
TWEET

Set.mjs

a guest Jun 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class Set {
  2.   constructor(args) {
  3.     const A = this;
  4.     Object.assign(A, ...(args || []).map((x) => ({ [x]: x })));
  5.   }
  6.  
  7.   equals(B) {
  8.     const A = this;
  9.     return A.every((x) => x in B) && B.every((x) => x in A);
  10.   }
  11.  
  12.   filter(f) {
  13.     const A = this;
  14.  
  15.     return A.map((x) => {
  16.       if (f(x)) {
  17.         return x;
  18.       }
  19.     });
  20.   }
  21.  
  22.   union(...args) {
  23.     const F = this;
  24.  
  25.     if (args.length) {
  26.       return new Set([F, ...args]).union();
  27.     }
  28.  
  29.     const A = new Set();
  30.  
  31.     F.forEach((Y) => {
  32.       Y.forEach((x) => {
  33.         A.add(x);
  34.       });
  35.     });
  36.  
  37.     return A;
  38.   }
  39.  
  40.   map(f) {
  41.     const A = this;
  42.     const B = new Set();
  43.  
  44.     A.forEach((x) => {
  45.       const y = f(x);
  46.  
  47.       if (y !== undefined) {
  48.         B.add(y);
  49.       }
  50.     });
  51.  
  52.     return B;
  53.   }
  54.  
  55.   powerset() {
  56.     const S = this;
  57.  
  58.     if (S.equals(new Set())) {
  59.       return new Set([new Set()]);
  60.     }
  61.  
  62.     const F = (e, T) => T.map((X) => X.union(new Set([e])));
  63.     const e = S.pick();
  64.     const T = S.difference(new Set([e]));
  65.     const PT = T.powerset();
  66.     return PT.union(F(e, PT));
  67.   }
  68.  
  69.   difference(A) {
  70.     const B = this;
  71.     return B.filter((x) => !(x in A));
  72.   }
  73.  
  74.   intersection(...args) {
  75.     const F = this;
  76.  
  77.     if (args.length) {
  78.       return new Set([F, ...args]).intersection();
  79.     }
  80.  
  81.     if (F.equals(new Set())) return;
  82.  
  83.     const B = F.pick();
  84.     return B.filter((x) => F.every((Y) => x in Y));
  85.   }
  86.  
  87.   product(B) {
  88.     const A = this;
  89.     return A.map((x) => B.map((y) => Set.pair(x, y)));
  90.   }
  91.  
  92.   every(f) {
  93.     const A = this;
  94.  
  95.     for (const x of A) {
  96.       if (!f(x)) {
  97.         return false;
  98.       }
  99.     }
  100.  
  101.     return true;
  102.   }
  103.  
  104.   some(f) {
  105.     const A = this;
  106.     return !A.every((x) => !f(x));
  107.   }
  108.  
  109.   pick() {
  110.     const A = this;
  111.  
  112.     for (const x of A) {
  113.       return x;
  114.     }
  115.   }
  116.  
  117.   toString() {
  118.     const A = this;
  119.     return `{${[...A]}}`;
  120.   }
  121.  
  122.   static pair(x, y) {
  123.     return new Set([new Set([x]), new Set([x, y])]);
  124.   }
  125.  
  126.   static N(n) {
  127.     if (n === 0) {
  128.       return new Set();
  129.     }
  130.  
  131.     return Set.N(n - 1).union(new Set([Set.N(n - 1)]));
  132.   }
  133.  
  134.   /* ES6 Set polyfill */
  135.  
  136.   get size() {
  137.     const A = this;
  138.     return [...A].length;
  139.   }
  140.  
  141.   static get [Symbol.species]() {
  142.     return Set;
  143.   }
  144.  
  145.   add(x) {
  146.     const A = this;
  147.     A[x] = x;
  148.     return A;
  149.   }
  150.  
  151.   clear() {
  152.     const A = this;
  153.  
  154.     A.forEach((x) => {
  155.       A.delete(x);
  156.     });
  157.   }
  158.  
  159.   delete(x) {
  160.     const A = this;
  161.     const has = A.has(x);
  162.     delete A[x];
  163.     return has;
  164.   }
  165.  
  166.   *entries() {
  167.     const A = this;
  168.  
  169.     for (const x of A) {
  170.       yield [x, x];
  171.     }
  172.   }
  173.  
  174.   forEach(f, ...args) {
  175.     const A = this;
  176.  
  177.     for (const x of A) {
  178.       f.call(args[0], x, x, A);
  179.     }
  180.   }
  181.  
  182.   has(x) {
  183.     const A = this;
  184.     return Object.prototype.hasOwnProperty.call(A, x);
  185.   }
  186.  
  187.   *[Symbol.iterator]() {
  188.     const A = this;
  189.  
  190.     for (const x of Object.values(A)) {
  191.       yield x;
  192.     }
  193.   }
  194. }
  195.  
  196. Object.defineProperties(Set.prototype, {
  197.   keys: {
  198.     value: Set.prototype[Symbol.iterator],
  199.   },
  200.   values: {
  201.     value: Set.prototype[Symbol.iterator],
  202.   },
  203. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top