diff --git a/src/sets.ts b/src/sets.ts new file mode 100644 index 0000000..517c391 --- /dev/null +++ b/src/sets.ts @@ -0,0 +1,13 @@ +export const union = (a: Set, b: Set) => new Set([...a, ...b]); +export const intersection = (a: Set, b: Set) => + new Set([...a].filter((x) => b.has(x))); +export const difference = (a: Set, b: Set) => + new Set([...a].filter((x) => !b.has(x))); +export const symmetric = (a: Set, b: Set) => + union(difference(a, b), difference(b, a)); +export const isSubsetOf = (a: Set, b: Set) => + [...b].every((x) => a.has(x)); +export const isSupersetOf = (a: Set, b: Set) => + [...a].every((x) => b.has(x)); +export const isDisjointFrom = (a: Set, b: Set) => + !intersection(a, b).size;