summaryrefslogtreecommitdiff
path: root/czzc/table.lua
blob: bb34f2f314838686dd3a6f71646d096f955e7880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local M = {}

-- shallow copies a table, dst optional.
M.copy = function(src, dst)
    dst = dst or {}
    for k, v in pairs(src) do
        dst[k] = v
    end
    return dst
end

-- recursively copies a table, dst optional.
-- might implode for infinitely nested tables.
M.deepcopy = function(src, dst)
    dst = dst or {}
    -- convenience.
    if type(src) ~= "table" then return src end
    for k, v in pairs(src) do
        dst[k] = M.deepcopy(v)
    end
    return dst
end

-- replaces all values of a table with fn(value).
M.map = function(fn, tbl)
    local res = M.copy(tbl)
    for k, v in pairs(tbl) do
        res[k] = fn(v)
    end
    return res
end

return M