Skip to content

Instantly share code, notes, and snippets.

@magicoal-nerb
Created October 15, 2025 05:47
Show Gist options
  • Save magicoal-nerb/f184ef22b6376538695ace7ec24529db to your computer and use it in GitHub Desktop.
Save magicoal-nerb/f184ef22b6376538695ace7ec24529db to your computer and use it in GitHub Desktop.
set
--!strict
local Set = {}
Set.__index = Set
export type Set<T> = typeof(setmetatable({} :: {
data: { [T]: boolean },
}, Set))
function Set.new()
return setmetatable({
data = {},
}, Set)
end
function Set.contains<T>(self: Set<T>, what: T): boolean
return self.data[what] ~= nil
end
function Set.insert<T>(self: Set<T>, what: T)
self.data[what] = true
end
function Set.delete<T>(self: Set<T>, what: T)
self.data[what] = nil
end
function Set.iterate<T>(self: Set<T>, callback: (T, ...any) -> (), ...: any)
for key in self.data do
callback(key, ...)
end
end
return Set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment