What techniques that everyone should know?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: What techniques that everyone should know?

Post by kikito »

If you move the if outside the loop, the function gets shorter:

Code: Select all

function shallowCopy(t)
    if type(t) ~= 'table' then return t end
    local result = {}
    for k, v in pairs(t) do
      result[k] = shallowCopy(v)
    end
    return result
end
Now let's add another level there: Keys can be tables, too:

Code: Select all

function shallowCopy(t)
    if type(t) ~= "table" then return t end
    local result = {}
    for k, v in pairs(t) do
      result[shallowCopy(k)] = shallowCopy(v)
    end
    return result
end
Finally, tables containing loops will make the function loop infinitely. The only way I've found to handle that is to parse the table in advance, looking for loops, and handling those differently than the default tables. But the code to handle that case is a bit long and boring.
When I write def I mean function.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: What techniques that everyone should know?

Post by HugoBDesigner »

tio wrote:
Zarty55 wrote:I'm not sure if this would make a shallow copy of a table, but I will put it here:

Code: Select all

function copyTable(t)
  return {unpack(t)}
end
Robin wrote:Yes, but it will make a shallow copy of a sequence. If your table contains any non-sequence key/value pairs and you want to copy those too, you'd use:

Code: Select all

function shallowCopy(t)
    local n = {}
    for k, v in pairs(t) do
        n[k] = v
    end
    return n
end
Works considering that the table doesn't have any inner tables ;) (It will work, but will point to the tables of the first list, which may not be the desirable behavior).

One option to duplicate these tables too would be a recursive call, like this:

Code: Select all

function shallowCopy(t)
    local n = {}
    for k, v in pairs(t) do
        if type(v) == "table" then
            n[k] = shallowCopy(v)
        else
            n[k] = v
        end
    end
    return n
end
I made this function once. But I made the sub-tables copy thing optional. Instead of making it always copy tables (sometimes I wanted to change those tables, not their copies), I made this:

Code: Select all

function shallowCopy(t, sub)
    local sub = sub or false
    local n = {}
    for k, v in pairs(t) do
        if type(v) == "table" and sub then
            n[k] = shallowCopy(v, true)
        else
            n[k] = v
        end
    end
    return n
end
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: What techniques that everyone should know?

Post by tio »

kikito wrote:(...)
Finally, tables containing loops will make the function loop infinitely. The only way I've found to handle that is to parse the table in advance, looking for loops, and handling those differently than the default tables. But the code to handle that case is a bit long and boring.
How can a Lua table have a loop?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: What techniques that everyone should know?

Post by Nixola »

Code: Select all

>math.math = math
>print(math.math.math.math.math.math.math.sin(0))
1
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: What techniques that everyone should know?

Post by tio »

Nixola wrote:

Code: Select all

>math.math = math
>print(math.math.math.math.math.math.math.sin(0))
1
I din't know it was possible :o:
Thanks for the tip :nyu:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What techniques that everyone should know?

Post by Robin »

Hint: we were talking about shallow copies. ;)

My function was correct.

If you want a deep copy, I'd suggest this:

Code: Select all

function deepcopy(t, cache)
    if type(t) ~= 'table' then
        return t
    end

    cache = cache or {}
    if cache[t] then
        return cache[t]
    end

    local new = {}

    cache[t] = new

    for key, value in pairs(t) do
        new[deepcopy(key, cache)] = deepcopy(value, cache)
    end

    return new
end
This deals correctly with tables forming any graph, not just trees.

Also, neither touches metatables. If you want them metatables, you have to decide if you want to copy those as well, or just do something like setmetatable(new, getmetatable(t)).
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests