• Subject: Re: [Challenge] Smallest table copy
• From: Hans Hagen <pragma@...>
• Date: Wed, 07 May 2014 22:17:35 +0200

```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)

-----------------------------------------------------------------