[Challenge] Smallest table copy

classic Classic list List threaded Threaded
28 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Philipp Janda
Am 08.05.2014 15:47 schröbte Thiago L.:
>
> I wonder if less than 239 chars is possible (maybe with some
> load()/loadstring() trickery?)

Sure. Using Paul's trick I'm currently at 230 bytes (234 if you don't
like global `next`). But it's based on my original proposal, so you
won't like it ...

But using `load` is an interesting idea (e.g. for avoiding the
`function` keyword) ...

Philipp



Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Pierre-Yves Gérardy
I doubt you can get an improvement with `load`.

    load"local a=..."

is three characters longer than

    function(a)end

You can cache the prologue in a variable, but two functions is too
little to gain from it:

L="local a,b,c=..."load(L.."")load(L.."")
function(a,b,c)endfunction(a,b)end
—Pierre-Yves


On Thu, May 8, 2014 at 4:57 PM, Philipp Janda <[hidden email]> wrote:

> Am 08.05.2014 15:47 schröbte Thiago L.:
>
>>
>> I wonder if less than 239 chars is possible (maybe with some
>> load()/loadstring() trickery?)
>
>
> Sure. Using Paul's trick I'm currently at 230 bytes (234 if you don't like
> global `next`). But it's based on my original proposal, so you won't like it
> ...
>
> But using `load` is an interesting idea (e.g. for avoiding the `function`
> keyword) ...
>
> Philipp
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Philipp Janda
Am 08.05.2014 17:24 schröbte Pierre-Yves Gérardy:

> I doubt you can get an improvement with `load`.
>
>      load"local a=..."
>
> is three characters longer than
>
>      function(a)end
>
> You can cache the prologue in a variable, but two functions is too
> little to gain from it:
>
> L="local a,b,c=..."load(L.."")load(L.."")
> function(a,b,c)endfunction(a,b)end

Yes, and we also rely on upvalues, so we would need the `function`
keyword in there after all ...
But for a moment it was a good idea.

> —Pierre-Yves

Philipp


>
>
> On Thu, May 8, 2014 at 4:57 PM, Philipp Janda <[hidden email]> wrote:
>> Am 08.05.2014 15:47 schröbte Thiago L.:
>>
>>>
>>> I wonder if less than 239 chars is possible (maybe with some
>>> load()/loadstring() trickery?)
>>
>>
>> Sure. Using Paul's trick I'm currently at 230 bytes (234 if you don't like
>> global `next`). But it's based on my original proposal, so you won't like it
>> ...
>>
>> But using `load` is an interesting idea (e.g. for avoiding the `function`
>> keyword) ...
>>
>> Philipp
>>



Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

phlnc8
In reply to this post by Pierre-Yves Gérardy

226! (If I am counting correctly :)

I started with Daniel's version and used loadstring and gsub to "compress" the repeated sequences...  (Didn't actually test the result :)

loadstring(('dA,t)if"table"~=type(a)thenBaCt={}r=r or{}r[a]=t Da[r[x]or d(x,r)]=r[y]or d(y,r)CBtCtable.copy={shallowA)b={}Db[x]=yCBbC,deep=d}'):gsub('%u',{A='=function(a,r',B=' return ',C=' end ',D='for x,y in next,a do '}))()

On May 8, 2014 11:25 AM, "Pierre-Yves Gérardy" <[hidden email]> wrote:
I doubt you can get an improvement with `load`.

    load"local a=..."

is three characters longer than

    function(a)end

You can cache the prologue in a variable, but two functions is too
little to gain from it:

L="local a,b,c=..."load(L.."")load(L.."")
function(a,b,c)endfunction(a,b)end
—Pierre-Yves


On Thu, May 8, 2014 at 4:57 PM, Philipp Janda <[hidden email]> wrote:
> Am 08.05.2014 15:47 schröbte Thiago L.:
>
>>
>> I wonder if less than 239 chars is possible (maybe with some
>> load()/loadstring() trickery?)
>
>
> Sure. Using Paul's trick I'm currently at 230 bytes (234 if you don't like
> global `next`). But it's based on my original proposal, so you won't like it
> ...
>
> But using `load` is an interesting idea (e.g. for avoiding the `function`
> keyword) ...
>
> Philipp
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Soni "They/Them" L.

On 08/05/2014 12:38, phlnc8 wrote:

226! (If I am counting correctly :)

I started with Daniel's version and used loadstring and gsub to "compress" the repeated sequences...  (Didn't actually test the result :)

loadstring(('dA,t)if"table"~=type(a)thenBaCt={}r=r or{}r[a]=t Da[r[x]or d(x,r)]=r[y]or d(y,r)CBtCtable.copy={shallowA)b={}Db[x]=yCBbC,deep=d}'):gsub('%u',{A='=function(a,r',B=' return ',C=' end ',D='for x,y in next,a do '}))()

You should change that a[r[x]or d(x,r)]=r[y]or d(y,r) to change the "t" not "a"...

Also it's not safe: _G.next,_G.type=nil,nil and it errors...

So I would try to find a way to make this smaller:

loadstring(('local k,o,d=next,type dA,t)if"table"~=o(a)thenBaCt={}r=r or{}r[a]=t Dt[r[x]or d(x,r)]=r[y]or d(y,r)CBtCtable.copy={shallowA)b={}Db[x]=yCBbC,deep=d}'):gsub('%u',{A='=function(a,r',B=' return ',C=' end ',D='for x,y in k,a do '}))()

On May 8, 2014 11:25 AM, "Pierre-Yves Gérardy" <[hidden email]> wrote:
I doubt you can get an improvement with `load`.

    load"local a=..."

is three characters longer than

    function(a)end

You can cache the prologue in a variable, but two functions is too
little to gain from it:

L="local a,b,c=..."load(L.."")load(L.."")
function(a,b,c)endfunction(a,b)end
—Pierre-Yves


On Thu, May 8, 2014 at 4:57 PM, Philipp Janda <[hidden email]> wrote:
> Am 08.05.2014 15:47 schröbte Thiago L.:
>
>>
>> I wonder if less than 239 chars is possible (maybe with some
>> load()/loadstring() trickery?)
>
>
> Sure. Using Paul's trick I'm currently at 230 bytes (234 if you don't like
> global `next`). But it's based on my original proposal, so you won't like it
> ...
>
> But using `load` is an interesting idea (e.g. for avoiding the `function`
> keyword) ...
>
> Philipp
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Pierre-Yves Gérardy
In reply to this post by Philipp Janda
On Thu, May 8, 2014 at 5:35 PM, Philipp Janda <[hidden email]> wrote:
> Am 08.05.2014 17:24 schröbte Pierre-Yves Gérardy:
>> L="local a,b,c=..."load(L.."")load(L.."")
>> function(a,b,c)endfunction(a,b)end
>
>
> Yes, and we also rely on upvalues, so we would need the `function` keyword
> in there after all ...
> But for a moment it was a good idea.

No need for upvalues, a Lua chunck really is a vararg function:

    add = load"local a,b = ... return a + b"
    add(1,2) --> 3

—Pierre-Yves

Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Philipp Janda
Am 08.05.2014 20:11 schröbte Pierre-Yves Gérardy:

> On Thu, May 8, 2014 at 5:35 PM, Philipp Janda <[hidden email]> wrote:
>> Am 08.05.2014 17:24 schröbte Pierre-Yves Gérardy:
>>> L="local a,b,c=..."load(L.."")load(L.."")
>>> function(a,b,c)endfunction(a,b)end
>>
>>
>> Yes, and we also rely on upvalues, so we would need the `function` keyword
>> in there after all ...
>> But for a moment it was a good idea.
>
> No need for upvalues, a Lua chunck really is a vararg function:
>
>      add = load"local a,b = ... return a + b"
>      add(1,2) --> 3

If you want to use the `type` (or `next`) function in there without
passing it in every call, then you need them as upvalues. E.g.:

     add = load[[
       local t = ...
       return function( a, b )
         if t( a ) == t( b ) then
           return a + b
         end
       end]]( type )

Personally, I usually don't localize all globals in toy programs, but
the OP seems to care. Better safe than sorry, I guess.

>
> —Pierre-Yves
>

Philipp



Reply | Threaded
Open this post in threaded view
|

Re: [Challenge] Smallest table copy

Pierre-Yves Gérardy
On Fri, May 9, 2014 at 12:21 AM, Philipp Janda <[hidden email]> wrote:

> Am 08.05.2014 20:11 schröbte Pierre-Yves Gérardy:
>
>> On Thu, May 8, 2014 at 5:35 PM, Philipp Janda <[hidden email]> wrote:
>>>
>>> Am 08.05.2014 17:24 schröbte Pierre-Yves Gérardy:
>>>>
>>>> L="local a,b,c=..."load(L.."")load(L.."")
>>>> function(a,b,c)endfunction(a,b)end
>>>
>>>
>>>
>>> Yes, and we also rely on upvalues, so we would need the `function`
>>> keyword
>>> in there after all ...
>>> But for a moment it was a good idea.
>>
>>
>> No need for upvalues, a Lua chunck really is a vararg function:
>>
>>      add = load"local a,b = ... return a + b"
>>      add(1,2) --> 3
>
>
> If you want to use the `type` (or `next`) function in there without passing
> it in every call, then you need them as upvalues. E.g.:
>
>     add = load[[
>       local t = ...
>       return function( a, b )
>         if t( a ) == t( b ) then
>           return a + b
>         end
>       end]]( type )
>
> Personally, I usually don't localize all globals in toy programs, but the OP
> seems to care. Better safe than sorry, I guess.

Indeed, I hadn't thought about that in this case (I actually use that
technique in two of my libraries).

—Pierre-Yves

12