lua-users home
lua-l archive

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



On 07/05/2014 12:46, Philipp Janda wrote:
Am 07.05.2014 17:13 schröbte Thiago L.:

On 07/05/2014 12:00, Philipp Janda wrote:
Am 07.05.2014 15:40 schröbte Thiago L.:
So I have a challenge for you all: A table copy script in less than 270
chars


239 bytes. Stole some of your tricks. Not extensively tested but seems
to work ...

Philipp

Uhh it doesn't handle replacing _G.pairs with nil...

That is true. Of course you could add a local one-letter alias for `pairs` at the cost of 4 extra characters. While you are at it, replace `pairs(a)` with `next,a ` which saves another byte. So it's about 242 bytes now.

Also I don't like that wrapper function nonsense...
(table.copy.shallow(t,1) etc... bugs and obfuscation...)

There are no wrapper functions. I just used one function to implement the other via an extra boolean parameter (which you suggested in your OP, btw.).

Both versions can't handle extra arguments correctly (my `shallow`, your `deep`), and both are properly obfuscated. I optimized specifically for code size which I thought was the challenge (obviously I misunderstood) ...
When you say my deep can't handle extra arguments correctly: you're right, it errors if you pass in anything but a table, nil or false! (the latter 2 get replaced with a table) It also lets you pass in your own table so you can get an oldkey->newkey map!

Your shallow would just silently deepcopy everything... and only error with an invalid 3rd argument...

Of course, I wouldn't write nor use either version under normal circumstances.

Philipp