Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Last active January 7, 2023 08:59
Show Gist options
  • Save cxmeel/83ec9784a6714c3c5b767a9bc9cb3b8b to your computer and use it in GitHub Desktop.
Save cxmeel/83ec9784a6714c3c5b767a9bc9cb3b8b to your computer and use it in GitHub Desktop.
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