Created
February 8, 2023 05:51
-
-
Save Xevion/2390702004ccd756ad7e272debed9a3c to your computer and use it in GitHub Desktop.
Typescript Set Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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