Skip to content

Instantly share code, notes, and snippets.

@m0tive
Created April 10, 2015 11:56
Show Gist options
  • Save m0tive/37e8ec0aae962af359f7 to your computer and use it in GitHub Desktop.
Save m0tive/37e8ec0aae962af359f7 to your computer and use it in GitHub Desktop.
array and hash for lua
-- I haven't tested these, they probably don't work
local object = {
type = function(self) return getmetatable(self).__index end,
}
local hash = {
data = function(self) return getmetatable(self).data end,
set = function(self, key, value) rawset(self.data, key, value) end,
get = function(self, key) return rawget(self.data, key) end,
}
setmetatable(hash, {
__index = object,
__call = function(_, t)
t = t or {}
return setmetatable(t, {
data = {},
__index = hash,
})
end,
})
local array = {
data = function(self) return getmetatable(self).data end,
-- TODO push, pop, shift, unshift, insert, remove
}
setmetatable(array, {
__index = object,
__call = function(_, t)
local data = setmetatable({}, { __index = array, })
return setmetatable({}, {
data = data,
__index = data,
__len = function(self) return #(self.data) end,
})
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment