Last active
January 7, 2023 08:59
-
-
Save cxmeel/83ec9784a6714c3c5b767a9bc9cb3b8b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type Set<T> = { [T]: true } | |
-- Asymmetric difference | |
local function difference<V>(set: Set<V>, ...: Set<V>): Set<V> | |
local diff = table.clone(set) | |
for index, nextSet in { ... } do | |
for value in nextSet do | |
diff[value] = nil | |
end | |
end | |
return diff | |
end | |
-- Symmetric difference | |
local function differenceSymmetric<V>(set: Set<V>, ...: Set<V>): Set<V> | |
local diff = table.clone(set) | |
for _, nextSet in { ... } do | |
for value in nextSet do | |
diff[value] = if diff[value] == nil then true else false | |
end | |
end | |
for value, keep in diff do | |
diff[value] = if keep then true else nil | |
end | |
return diff | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment