Skip to content

Instantly share code, notes, and snippets.

@MattSeen
Forked from MihailJP/d_copy.lua
Created February 16, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSeen/9037450 to your computer and use it in GitHub Desktop.
Save MattSeen/9037450 to your computer and use it in GitHub Desktop.
function clone (t) -- deep-copy a table
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do
if type(v) == "table" then
target[k] = clone(v)
else
target[k] = v
end
end
setmetatable(target, meta)
return target
end
function copy (t) -- shallow-copy a table
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do target[k] = v end
setmetatable(target, meta)
return target
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment