Skip to content

Instantly share code, notes, and snippets.

@asim
asim / b.c
Created November 29, 2010 14:26
binary search in C without looking at the implementation
#include <stdio.h>
int bsearch(int left, int right, int find, int *a) {
if (left > right)
return -1;
int mid = (left + right) / 2;
if (find == a[mid])
@asim
asim / loop.sh
Created December 3, 2010 18:50
integer for loops in bash
for i in {1..100}; do echo foo; done
for ((i=1; i<=100; i++)); do echo foo; done
for `seq 1 100`; do echo foo; done
# while version
i=1; while [ $i -le 100 ]; do echo foo; i=$(($i+1)); done
@asim
asim / foo.lua
Created December 4, 2010 21:52
just writing some lua
local store = {}
local function add(key, value)
store[key] = value
end
local function get(key)
return store[key]
end
@asim
asim / keystore.lua
Created December 5, 2010 21:52
key value store, the humble beginnings
local config = { host="localhost", port=6379 }
local socket = require("socket")
local server = assert(socket.bind(config.host, config.port))
local ip, port = server:getsockname()
print(string.format("listening on %s:%s", ip, port))
local store = {}
local function add(key, value)
store[key] = value
@asim
asim / maillog.rb
Created December 9, 2010 17:16
ugly code
#!/usr/local/ruby/bin/ruby
require "rubygems"
require "redis"
require "eventmachine"
require "eventmachine-tail"
LOG = "/tmp/foo"
R = Redis.new(:host => "10.1.10.2")
class Reader < EventMachine::FileTail
@asim
asim / stats.rb
Created December 15, 2010 15:51
stats class
class Stats
QUEUES = ["bounced", "connect", "conversations", "deferred", "host", "lost", "sent"]
def self.lookup(pattern)
h = {}
REDIS.keys(pattern).map { |k,v| h[k] = REDIS[k] }
h
end
def self.day_for(ip)
# method should return a hash of count queues for 24 hours
h = {}
Stats::QUEUES.size.times do |i|
a[i] = Thread.new {
queue = Stats::QUEUES[i-1]
h["#{queue}:#{ip}:24"] = return_day_for(ip,queue)
}
end
@asim
asim / foo.sh
Created January 17, 2011 15:29
save
# LOL!!1
alias wtf='dmesg'
alias onoz='cat /var/log/errors.log'
alias rtfm='man'
alias visible='echo'
alias invisible='cat'
alias moar='more'
@asim
asim / ev.c
Created March 30, 2011 11:49
ignore this
#include <stdio.h>
#include <event.h>
#include <evhttp.h>
#include <stdlib.h>
struct server {
struct event_base *base;
struct evhttp *http;
};
@asim
asim / foo.c
Created April 17, 2011 19:30
nothing special
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct array {
int pos;
char *pointers[1024];
};
static struct array arr;