Skip to content

Instantly share code, notes, and snippets.

@MillaBasset
MillaBasset / debugmetas.lua
Created October 28, 2021 01:57
Fun things to do with `debug.setmetatable`. Includes conveniences for numbers, strings, and functions.
-- numbers: return the number when called, like a nilad
debug.setmetatable(0, {
__call = function(n) return n end
})
-- strings: execute the string as Lua code when called
debug.setmetatable("", {
__call = function(s)
return assert((loadstring or load)(s))()
end
@MillaBasset
MillaBasset / indexupvalues.lua
Created October 27, 2021 18:58
Allows you to index function upvalues with the normal `myfunc[1]` syntax.
debug.setmetatable(function() end, {
__index = function(f, k)
local t = {debug.getupvalue(f, k)}
return {name = t[1], value = t[2]}
end,
__newindex = debug.setupvalue
})
@MillaBasset
MillaBasset / newxpcall.lua
Created October 27, 2021 16:06
New xpcall function that allows the called function to take arguments.
do
local old_xpcall = xpcall
function xpcall(f, err, ...)
local arg = {...}
return old_xpcall(function()
return f((unpack or table.unpack)(arg))
end, err)
end
end
@MillaBasset
MillaBasset / newclass.lua
Last active October 25, 2021 01:14
Simple class creation method in the style of Programming in Lua: 1st Edition, Section 16.3. Supports multiple inheritance and modern versions of Lua (up to 5.4 and probably into the future.)
-- look up for `k' in list of tables `plist'
local function search(k, plist)
for i = 1, #plist do
local v = plist[i][k] -- try `i'-th superclass
if v then return v end
end
end
return function(...)
local c = {} -- new class
@MillaBasset
MillaBasset / printf.lua
Created October 22, 2021 03:56
... Literally just printf. Really simple. Could add a "post-function" argument; that is, something to replace "print" if you want.
return function(...)
return print(string.format(...))
end
@MillaBasset
MillaBasset / newprint.lua
Created October 21, 2021 14:37
A new print function that returns what it prints as a string.
do
function print(...)
local t = {...}
for k, v in pairs(t) do
t[k] = tostring(v)
io.write(t[k], "\n")
end
return table.concat(t, "\n")
end
end
@MillaBasset
MillaBasset / list.lua
Last active September 28, 2021 12:48
Toy implementation of a stack/queue/double queue, as described in Programming in Lua: 1st Edition, Section 11.4. All operations are performed in constant time.
local list = {}
function list.pushLeft(l, value)
local first = l.first - 1
l.first = first
l[first] = value
end
function list.pushRight(l, value)
local last = l.last + 1
@MillaBasset
MillaBasset / time.lua
Last active October 24, 2024 18:20
Time your code! Return values are time taken to run the function followed by any return values the function might have.
local function isCallable(o)
if type(o) == "function" then
return true
elseif type(o) == "table" then
local mt = getmetatable(o)
return mt and isCallable(mt.__call)
else
return false
end
end
@MillaBasset
MillaBasset / schrodinger.lua
Last active September 21, 2021 19:46
Have you ever wanted your booleans to misbehave? Now they will, with a random chance! Credit to ry00001 for this.
local the_new_env = {}
math.randomseed(os.time())
setmetatable(_G, {
__index = function(_, k)
local v = the_new_env[k]
if type(v) == "boolean" and math.random(5) == 1 then
v = not v
end
return v
end,
@MillaBasset
MillaBasset / newtype.lua
Last active September 21, 2021 17:56
A new type function that takes into account the __type field in an object's metatable to return a custom type - useful if you want to distinguish different types of objects, rather than having all of them simply be "table".
do
local oldType = type
function type(o)
local mt = getmetatable(o)
if mt and mt.__type then
return tostring(mt.__type)
else
return oldType(o)
end
end