This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return function(...) | |
return print(string.format(...)) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder