Skip to content

Instantly share code, notes, and snippets.

@RhodiumToad
Last active February 6, 2018 09:01
Show Gist options
  • Save RhodiumToad/de73681e463d5d2e60f42ff906a4eeeb to your computer and use it in GitHub Desktop.
Save RhodiumToad/de73681e463d5d2e60f42ff906a4eeeb to your computer and use it in GitHub Desktop.
function dcopy(orig_t)
if type(orig_t) ~= "table" then return orig_t end
--[[
Recurse with memoization; t is always a table, if it's a table
previously seen then return the memoized copy, otherwise make
a new empty table and copy
--]]
local memot = {}
local function dcopy_rec(t)
local nt = memot[t]
if nt ~= nil then return nt end
nt = {}
memot[t] = nt
for k,v in pairs(t) do
if type(v) == "table" then
nt[k] = dcopy_rec(v)
else
nt[k] = v
end
end
return nt
end
return dcopy_rec(orig_t)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment