Created
August 6, 2021 02:36
-
-
Save b0o/722be3414c4dec5f2aa4b1b9512481b1 to your computer and use it in GitHub Desktop.
Operations over sets
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
const union = (...sets) => new Set(sets.flatMap(s => [...s])) | |
const intersection = (a, b, ...rest) => (rest.length ? intersection : (s => new Set(s)))([...a].reduce((acc, e) => b.has(e) ? [...acc, e] : acc, []), ...rest) | |
const difference = (...sets) => [...intersection(...sets)].reduce((acc, e) => !acc.delete(e) || acc, union(...sets)) | |
const leftDifference = (left, ...rights) => difference(left, intersection(left, union(...rights))) | |
const rightDifference = (...sets) => leftDifference(...sets.reverse()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment