Last active
February 6, 2018 09:01
-
-
Save RhodiumToad/de73681e463d5d2e60f42ff906a4eeeb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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