Skip to content

Instantly share code, notes, and snippets.

@newhouseb
newhouseb / C++ continuations
Created July 12, 2010 14:47
Generator pattern with only pthreads and macros (generator.h is ~80 LOC - will publish after some cleaning)
#include <stdio.h>
#include "generator.h"
generator(int, counter, int start; int other)
{
while(1) {
yield(arguments.start++);
yield(arguments.other);
}
}
@newhouseb
newhouseb / gist:593214
Created September 23, 2010 06:16
Makefile Unit Tests
test_file:
echo "#include <stdio.h>" > test.c
awk '/^bool test_(.*)\(\)/ {print "extern bool", $$2, ";"}' *.c >> test.c
echo "int unit_test(){ bool pass; int cases = 0, passes = 0;" >> test.c
awk '/^bool test_(.*)\(\)/ {print "pass =",$$2,"; printf(\"%s", $$2, "\\n\", pass ? \"PASSED\" : \"FAILED\"); cases += 1; if(pass) passes++;"}' *.c >> test.c
echo "printf(\"%i of %i cases PASSED\n\", passes, cases);}" >> test.c
test: CFLAGS += -Dmain\(args...\)="* a_; extern int unit_test(); main() { unit_test(); } int b_(args)" test.c
test: EXECUTABLE = test
test: test_file all
./$(EXECUTABLE)
def setup_scribe():
# Tested on a fresh Ubuntu 10.10 64-bit server
sudo('apt-get install python-dev libevent-1.4-2 libevent-dev ' +
'libboost-dev libtool build-essential automake libglib2.0-0 ' +
'libglib2.0-dev autoconf libboost-all-dev flex bison git')
with settings(warn_only=True):
run('rm -rf thrift')
run('git clone https://github.com/apache/thrift.git')
class CometConnections(tornado.web.RequestHandler):
connections = set({})
@tornado.web.asynchronous
def get(self):
CometConnections.connections.add(self)
def tell(self, js):
if self in CometConnections.connections:
CometConnections.connections.remove(self)
{
"status": "I am a unit test!"
}
@newhouseb
newhouseb / gist:1224905
Created September 18, 2011 09:30
Every possible form of syntax in JS
true;
null;
NaN;
undefined;
21.2;
"hello";
window;
([1,2,3]);
({ a:2, 'b':3});
/1/g;
@newhouseb
newhouseb / gist:1250398
Created September 29, 2011 09:35
makewatch

A bash function that makes a makefile every time you save a file that make depends on.

First install inotify-tools if you haven't already:

apt-get install inotify-tools

Then put this in your .bashrc (or just run it in your current session).

function makewatch() { while true; do make $1; strace -e trace=stat make -q 2>&1 | sed 's/^[^"]*"//' | grep " = 0" | sed 's/".*//' | xargs inotifywait -e create -e modify; done }

@newhouseb
newhouseb / gist:1620133
Created January 16, 2012 10:20
MySQL vs PostgreSQL Schema changes benchmarks
The basic idea here is to substantiate the claims made by this square post:
http://corner.squareup.com/2011/06/postgresql-data-is-important.html
In PostgreSQL, and MySQL (MyISAM and InnoDB) I create millions of rows and then add
and remove columns and add and remove indexes. For columns without defaults this is
basically free in PostgreSQL and O(n) in MySQL. For adding indexes its at best O(n)
everywhere, but with PostgreSQL it claims not to do any locking that would otherwise
prevent table interaction.
Also, PostgreSQL has _awsome_ documentation (it has real examples!). I always get
@newhouseb
newhouseb / speedtest
Created February 19, 2012 07:56
DIY Speedtest
# curl http://[yourhost]:2014 > /dev/null
# This will show average download speed for infinity client-side, until you ctrl-c
# The server prints a rolling average of the connection speed (can only accept one connection at a time)
import socket
import time
host = ''
port = 2014
backlog = 5
@newhouseb
newhouseb / gist:3607474
Created September 3, 2012 07:03
Core Data being goofy

If you put this code into AppDelegate.m in a blank XCode project with coredata enabled (i.e. the checkbox that generates the boilerplate checked).

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"Length %i", [[object valueForKey:@"employees"] count]);
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{