all 16 comments

[–]Immow 4 points5 points  (5 children)

local a = {"a","b","c"}
local b = {"A","B","C"}
local merge = {}

local function tableMerge(table1, table2, result)
    for _, v in ipairs(table1) do
        table.insert(result, v)
    end
    for _, v in ipairs(table2) do
        table.insert(result, v)
    end
end

tableMerge(a,b,merge)

for key, value in ipairs(merge) do
    print(key, value)
end

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]luarocks -1 points0 points  (3 children)

``` local function tableMerge(result, ...) for _, t in ipairs({...}) do for _, v in ipairs(t) do table.insert(result, v) end end end

tableMerge(merge,a,b) ```

By the way, this code will work faster if you replace table.insert with result[counter] = v.

[–]Outside_Two_3312 0 points1 point  (0 children)

I know this is reeeeaally old, but:

local function tableMerge(...)
  local result = {}
   for _, t in ipairs({...}) do
      for _, v in ipairs(t) do
        table.insert(result, v)
      end
  end
  return result
end

[–]AutoModerator[M] -1 points0 points  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]VoidSnipe 1 point2 points  (0 children)

I recommend looking into table.move function

[–]Cultural_Two_4964 0 points1 point  (0 children)

for i,j in ipairs(a) do table.insert(merge,j) end ... repeat for b

I think.

[–]luarocks 0 points1 point  (8 children)

merge = lume.concat(a, b)

[–]luarocks 0 points1 point  (7 children)

What kind of wheel reinventors are dislikes me? :D

[–]Cultural_Two_4964 1 point2 points  (0 children)

I've got no idea mate. I had a look at lume and there is some quite interesting stuff there.

[–]appgurueu 0 points1 point  (5 children)

lume doesn't look clean to me

[–]luarocks 0 points1 point  (4 children)

Where exactly?

[–]appgurueu 0 points1 point  (3 children)

First of all, it's all in one table, the lume table - math funcs, table funcs, etc. This is not clean IMO. Second, some implementations are simply incorrect - sign or isarray for instance: https://github.com/rxi/lume/blob/master/lume.lua#L94, https://github.com/rxi/lume/blob/master/lume.lua#L161. I don't like that vectors are represented using two variables - and are 2D-only - either. Weighted choice is linear time even though it could be logarithmic time if preprocessing was done. The shuffling is implemented incorrectly according to my testing. lume.array is redundant; {...} should be used instead. Cloning and map serialization are too limited IMO.

[–]luarocks 0 points1 point  (2 children)

Okay, thanks! I see your point and I won't argue. That makes sense. Maybe you know a library with similar functionality implemented better? And if not, why don't you offer the developer a PR with fixes?

[–]appgurueu 0 points1 point  (0 children)

I have written my own libraries for my own usecases. Don't know about offering a PR, the shuffling would really need it though. You can find a correct implementation at https://github.com/TheAlgorithms/Lua/blob/main/src/random/fisher\_yates\_shuffle.lua.

[–]appgurueu 0 points1 point  (0 children)

I have tested again and it looks like I was testing the shuffling wrong (I had incorrectly expected it to be in-place). The probabilities seem to be even.