Created
April 10, 2015 11:56
-
-
Save m0tive/37e8ec0aae962af359f7 to your computer and use it in GitHub Desktop.
array and hash for lua
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
-- 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