-
-
Save ryanpcmcquen/7aca8ba7f9bce67d3a375fee72094cf3 to your computer and use it in GitHub Desktop.
local function combineTables(...) | |
local combinedTable = {} | |
local arg = {...} | |
for k, v in pairs(arg) do | |
if type(v) == 'table' then | |
for tk, tv in pairs(v) do | |
table.insert(combinedTable, tv) | |
end | |
end | |
end | |
return combinedTable | |
end |
More performant?
@ha6000, that is functionally different than combineTables
:
https://repl.it/@ryanpcmcquen/PeacefulGeneralKeyboard#main.lua
one = { 'ctrl', 'alt' }
two = { 'shift' }
print_it(combineTables(one, two))
--[[
1 ctrl
2 alt
3 shift
--]]
print_it(table.assign(one, two))
--[[
1 shift
2 alt
--]]
yes? but more close to Object.assign
@ha6000, I should have clarified, I am thinking of JavaScript's Object.assign()
:
https://repl.it/@ryanpcmcquen/CoarseCorruptDiskdrive
Object.assign(
{ 1: 'ctrl', 2: 'alt' },
{ 3: 'shift' }
);
// { 1: 'crtl', 2: 'alt', 3: 'shift' }
but, I would say mine is more close to object.assign, I might be understanding you incorrectly by what you mean.
const one = ['ctrl', 'alt'];
const two = ['shift'];
console.log(Object.assign(one, two));
// [ 'shift', 'alt' ]
Object.assign
isn't meant to apply to arrays. What are you trying to accomplish with your snippet?
Yeah I agree with that, but my code would do the same as Object.assign would do, which is what the gist is trying to do (I think, but this is what came up when searching for that).
This gist is more like concat
It wouldn't do the same things for objects though. The intention of the gist is to combine tables.
This does for tables what Object.assign
does for objects.
I think this works better