Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created February 8, 2023 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xevion/2390702004ccd756ad7e272debed9a3c to your computer and use it in GitHub Desktop.
Save Xevion/2390702004ccd756ad7e272debed9a3c to your computer and use it in GitHub Desktop.
Typescript Set Functions
export const union = <T>(a: Set<T>, b: Set<T>) => new Set([...a, ...b]);
export const intersection = <T>(a: Set<T>, b: Set<T>) => new Set([...a].filter(x => b.has(x)));
export const difference = <T>(a: Set<T>, b: Set<T>) => new Set([...a].filter(x => !b.has(x)));
export const symmetric = <T>(a: Set<T>, b: Set<T>) => union(difference(a, b), difference(b, a));
export const isSubsetOf = <T>(a: Set<T>, b: Set<T>) => [...b].every(x => a.has(x));
export const isSupersetOf = <T>(a: Set<T>, b: Set<T>) => [...a].every(x => b.has(x));
export const isDisjointFrom = <T>(a: Set<T>, b: Set<T>) => !intersection(a, b).size;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment