Skip to content

Instantly share code, notes, and snippets.

@dredozubov
dredozubov / bithacks.c
Created January 31, 2013 14:09
bit manipulation helpers in C
static unsigned long POWERS_OF_TWO[] = {
1,2,4,8,16,32,64,128,
256,512,1024,2048,4096,8192,16384,32768,
65536,131072,262144,524288,1048576,2097152,4194304,8388608,
16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648LL
// 2147483648 = 2**31 isn't handled well by gcc, should explicitly go with 2147483648LL
};
// for testing and debug output purposes
@dredozubov
dredozubov / ruby_inject.rb
Created February 14, 2013 10:47
single ruby inject(reduce) implementation
module Enumerable
def my_inject(default=0)
self.each do |i|
default = yield(default, i)
end
default
end
end
puts [1, 2, 3].my_inject { |a, b| a + b }.inspect
@dredozubov
dredozubov / dispatcher.coffee
Created February 18, 2013 12:33
coffeescript dynamic method dispatcher
class Validator
validate: (questionId) ->
self = this
question = $("##{questionId}")
questionType = question.attr('type')
method = toCamelCase("validate_#{questionType}")
if @[method]
@[method](questionId)
else
console.log "missing validator for #{}"
@dredozubov
dredozubov / singleton.py
Created March 5, 2013 11:15
singleton decorator
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@dredozubov
dredozubov / not.rb
Created March 11, 2013 19:23
ruby dsl'ly :not method
class Object
def not
Not.new self
end
class Not
def initialize(original)
@original = original
end
@dredozubov
dredozubov / dfs.py
Created April 23, 2013 09:27
def dfs(g, start): stack, enqueued = [(None, start)], set([start]) while stack: parent, n = stack.pop() yield parent, n new = set(g.get(n, set())) - enqueued enqueued |= new stack.extend([(n, child) for child in new])
def dfs(g, start):
stack, enqueued = [(None, start)], set([start])
while stack:
parent, n = stack.pop()
yield parent, n
new = set(g.get(n, set())) - enqueued
enqueued |= new
stack.extend([(n, child) for child in new])
@dredozubov
dredozubov / subl.sh
Created May 12, 2013 08:16
subl launcher OS X
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
fn map<T, U>(list: &[T], f: &fn(T) -> U) -> ~[U] {
let mut acc = ~[];
for i in list.iter() {
acc.push(f(i));
}
acc
}
fn print_elems(list: &[int]) {
for i in list.iter() { println(fmt!("%?", i)); }
extern mod std;
use std::cmp::{TotalEq, Eq};
pub struct Foo {
priv bar: int
}
impl TotalEq for Foo {
fn equals(&self, other: &Foo) -> bool {
self.bar == other.bar
#!/bin/bash
# node.js using PPA (for statsd)
sudo apt-get install -y python-software-properties
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y nodejs npm
# Install git to get statsd
sudo apt-get install -y git