[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Challenge] Smallest table copy
- From: "Thiago L." <fakedme@...>
- Date: Thu, 08 May 2014 10:47:13 -0300
On 07/05/2014 18:52, Daniel Stephens wrote:
On 5/7/14, 2:19 PM, Paul K wrote:
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
replace "table" with o{} to save few more bytes...
Paul.
It's actually shorter to keep the constant string but not assign type
to a local, since it's only called once - at 238:
local k,d=next d=function(a,r,t)if"table"~=type(a)then return a end
t={}r=r or{}r[a]=t for x,y in k,a do a[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}
Not sure why you're doing a[r[x]or d(x,r)]=r[y]or d(y,r)... it should be
t[r[x]or d(x,r)]=r[y]or d(y,r)...
But I think we have a winner, with 238 chars:
local k,d=next d=function(a,r,t)if"table"~=type(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}
Or a bug free version, with 239 chars:
local k,o,d=next,type d=function(a,r,t)if o{}~=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}
I wonder if less than 239 chars is possible (maybe with some
load()/loadstring() trickery?)