lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 07/05/2014 17:17, Hans Hagen wrote:
On 5/7/2014 7:46 PM, Daniel Stephens wrote:
Your original submission can be shortened by making deep copy deal with
the 'is this a table' question on its input and using it always:

local k,o,n,s,d=pairs,type,"table"s,d=function(a,b)b={}for x,y in k(a)do
b[x]=y end return b end,function(a,r,t)if n~=o(a)then return a end
t={}r=r or{}r[a]=t for x,y in k(a)do x=r[x]or d(x,r)y=r[y]or
d(y,r)t[x]=y end return t end table.copy={shallow=s,deep=d}

one byte less:

local k,o,n,s,d=next,type,"table"s,d=function(a,b)b={}for x,y in k,a do b[x]=y end return b end,function(a,r,t)if n~=o(a)then return a end t={}r=r or{}r[a]=t for x,y in k,a do x=r[x]or d(x,r)y=r[y]or d(y,r)t[x]=y end return t end table.copy={shallow=s,deep=d}

and a few less:

local k,o,s,d=next,type s,d=function(a,b)b={}for x,y in k,a do b[x]=y end return b end,function(a,r,t)if"table"~=o(a)then return a end t={}r=r or{}r[a]=t for x,y in k,a do x=r[x]or d(x,r)y=r[y]or d(y,r)t[x]=y end return t end table.copy={shallow=s,deep=d}

(given that you're after less bytes of code)

-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
    tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                             | www.pragma-pod.nl
-----------------------------------------------------------------

I managed to get 242 bytes/chars... I could probably do less tho...
local k,o,d=next,type d=function(a,r,t)if"table"~=o(a)then return a end t={}r=r or{}r[a]=t for x,y in k,a do t[r[x]or d(x,r)]=r[y]or d(y,r)end return t end table.copy={shallow=function(a,b)b={}for x,y in k,a do b[x]=y end return b end,deep=d}