Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
JakobOvrum / gist:707429
Created November 19, 2010 23:46
Concatify: less garbage, less copying, better complexity
if data then
local where = {}
local k, v = next(data, nil)
if k then
insert(where, concat{k, '=', sql_tostring(v)})
for k, v in next, data, k do
insert(where, concat{' AND ', k, '=', sql_tostring(v)})
end
end
@JakobOvrum
JakobOvrum / gist:760600
Created December 31, 2010 01:33
Loco test.lua
local loco = require "loco"
local gen = loco.new()
gen:feed("init.lua")
gen:write("gh-pages")
@JakobOvrum
JakobOvrum / gist:817759
Created February 9, 2011 02:11
varargs example
Command "cmd"
{
function(...)
local args = {...}
for k, arg in pairs(args) do
reply("Arg #%d: %s", k, arg)
end
end
}
Command "imdb"
{
expectedArgs = "^%s*(.-)%s*$";
function(search)
search = search:gsub("[^%w ]", "")
reply("search: %s", search)
end
}
@JakobOvrum
JakobOvrum / tableprint.lua
Created February 9, 2011 03:28
table.print function with custom sink support and circular reference detection.
local pairs = pairs
local type = type
local tostring = tostring
local print = print
local table = table
local next = next
module "tableprint"
local function tableprint(t, tab, sink, cache)
@JakobOvrum
JakobOvrum / waitforauth.lua
Created February 11, 2011 06:42
put in on_connect to wait for gamesurge auth
--do not put entries into the channels table!
--replace with your account password
local authpass = "YOURPASSWORD"
--gamesurge auth
function on_connect(bot)
bot:sendChat("AuthServ@Services.GameSurge.net", "IDENTIFY " .. authpass)
bot:setMode{add = "x"} -- for mask
bot:hook("OnNotice", "auth", function(user, channel, message)
--Say "Hi!", wait 3 seconds, say "I'm a bot!", wait 10 seconds, repeat
do
local spamchannel
--Think hooks are run once every bot:think(), but can be delayed with the "wait" function.
--The "wait" function does not block the rest of the program.
Hook "Think"
{
enabled = false; --not enabled by default
@JakobOvrum
JakobOvrum / gist:1950300
Created March 1, 2012 15:00
Range!() template for static range foreach
template Range(int from, int to) if(from < to)
{
alias RangeImpl!(from, to - 1) Range;
}
template RangeImpl(int lowerLimit, int iterator, tail...)
{
static if(iterator < lowerLimit)
alias tail RangeImpl;
else
@JakobOvrum
JakobOvrum / thiscall.d
Created April 16, 2012 23:09
thiscall
import std.traits;
// Simplified version; does not work with a large number of possible types for F
private extern(C++) interface Thiscall(F)
{
ReturnType!F cppfunc(ParameterTypeTuple!F args);
}
auto thiscall(F, Args...)(void* _this, F func, Args args)
{
// Example options
struct CmdOptions
{
/// Print information during execution.
bool verbose = false;
/// Output name.
string outputname = "foo";
}