Skip to content

Instantly share code, notes, and snippets.

View revolucas's full-sized avatar

Alundaio revolucas

View GitHub Profile
@revolucas
revolucas / table_merge.lua
Created April 30, 2018 02:35
Merge two tables recursively without worry of 'stack overflow'
function table_merge(into,from)
local stack = {}
local node1 = into
local node2 = from
while (true) do
for k,v in pairs(node2) do
if (type(v) == "table" and type(node1[k]) == "table") then
table.insert(stack,{node1[k],node2[k]})
else
node1[k] = v
--[[
author: Alundaio (aka Revolucas)
A function that creates a new class-like table suitable for combining table properties.
It is very simple, only 20 lines and it is very aesthetically pleasing to define new 'classes'
ex 1. Class "new_class"
ex 2. Class "new_class" (inherit)
ex 3. Class "new_class" (inherit1,inherit2,...)
The only requirement is that you need to give _G a metatable where 'this' returns the table
being indexed, which in my opinion is useful on it's own as it will return the current table
@revolucas
revolucas / print_table.lua
Last active April 16, 2024 15:13
[Lua] Print table function for deeply nested tables
--[[
Most pure lua print table functions I've seen have a problem with deep recursion and tend to cause a stack overflow when
going too deep. This print table function that I've written does not have this problem. It should also be capable of handling
really large tables due to the way it handles concatenation. In my personal usage of this function, it outputted 63k lines to
file in about a second.
The output also keeps lua syntax and the script can easily be modified for simple persistent storage by writing the output to
file if modified to allow only number, boolean, string and table data types to be formatted.
author: Alundaio (aka Revolucas)