diff options
author | heav <hheav3@gmail.com> | 2022-10-27 00:47:27 +0000 |
---|---|---|
committer | heav <hheav3@gmail.com> | 2022-10-27 00:47:27 +0000 |
commit | 806af5e24024f499666687b702e04704cac3bd64 (patch) | |
tree | 67d929c9c0005d120cfe6692b4e949310272c60f | |
parent | 66cdb19b08ad8a19cc6e05450b71bdde5ee163f5 (diff) |
added table library
-rw-r--r-- | table.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/table.lua b/table.lua new file mode 100644 index 0000000..bb34f2f --- /dev/null +++ b/table.lua @@ -0,0 +1,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 |