Skip to content

Instantly share code, notes, and snippets.

-- Prosody IM
-- Copyright (C) 2008-2009 Matthew Wild
-- Copyright (C) 2008-2009 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
local datamanager = require "util.datamanager";
local log = require "util.logger".init("usermanager");
<?php
/**
* A command line interface for automatic downloads of image feeds
*
* @package easypopulate
* @author Waqas Hussain
* @copyright 2010
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License (v2 only)
*/
local setmetatable = setmetatable;
local pcall = pcall;
local type = type;
local t_concat = table.concat;
local print = print;
local socket_url = require "socket.url";
local http = require "socket.http";
local ltn12 = require "ltn12";
@zeen
zeen / lxp.lua
Created September 4, 2010 20:13
local coroutine = coroutine;
local tonumber = tonumber;
local string = string;
local setmetatable, getmetatable = setmetatable, getmetatable;
local pairs = pairs;
local deadroutine = coroutine.create(function() end);
coroutine.resume(deadroutine);
local gettime = require "socket".gettime;
module "shaper"
local shaper = {};
function shaper:update()
local newt = gettime();
local elapsed = newt - self.t;
@zeen
zeen / ini.lua
Created November 1, 2010 13:42
INI parser
function ini_parse(text)
text = "[]\n"..text:gsub(";[^\n]*\n", ""); -- remove comments and add dummy section
local result = {};
local section = "";
local lineno = 0;
for line in text:gmatch("([^\n]+)") do
lineno = lineno + 1;
local block = line:match("^%[([^%]*)%]$");
if block then
section = block;
@zeen
zeen / mod_muc_broadcast.lua
Created November 6, 2010 11:40
Messages sent to room bare JIDs by room owners get broadcasted
-- mod_muc_broadcast.lua
local jid_bare = require "util.jid".bare;
local st = require "util.stanza";
module:hook("message/bare", function(event)
local stanza = event.stanza;
if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
local from = jid_bare(stanza.attr.from);
@zeen
zeen / waqas.MattyMath.nocache.js
Created November 28, 2010 00:24
Adds Math.frexp, Math.ldexp
function waqas_MattyMath(){
var $wnd_0 = window, $doc_0 = document, gwtOnLoad, bodyDone, base = '', metaProps = {}, values = [], providers = [], answers = [], onLoadErrorFunc, propertyErrorFunc;
if (!$wnd_0.__gwt_stylesLoaded) {
$wnd_0.__gwt_stylesLoaded = {};
}
if (!$wnd_0.__gwt_scriptsLoaded) {
$wnd_0.__gwt_scriptsLoaded = {};
}
function isHostedMode(){
var result = false;
@zeen
zeen / ev.lua
Created November 30, 2010 07:26
lua-ev emulation using socket.select
-- lua-ev emulation using socket.select
local READ, WRITE = 0x01, 0x02;
local timeout = 5;
local Loop = {};
function Loop.new()
print("Loop:new")
I have DNS records with weights. Need to select/sort randomly by weight. Problem: Distribution of the random variable I have is undefined.
Current plan:
Get random number X = N uniformly random bits from rand(0,1)<0.5 (von Neumann's fair coin from unfair).
2^N >= sum(weights). X <= sum(weights). X would be uniform over sum(weights). Right?
Select kth number from list such that sum(Weight1...Weightk)>X.
1. Is this correct?
2. Is there a better way (I'd like to minimize rand() calls and processing in general)?
3. von Neumann's fair coin method works with two coin tosses for every fair toss. Can I reuse the last toss of the old pair in the next? That would cut rand() down by half.