Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / ring.erl
Created July 20, 2012 16:07
Grow your own Erlang process ring
-module(ring).
-export([chain/0, chain/1, start/3]).
chain() ->
chain(self()).
chain(Neighbor) ->
receive
stop ->
io:format("stopping ~p...~n", [self()]),
@cqfd
cqfd / singleton_stuff.rb
Created August 6, 2012 17:46
Ruby goofiness with singleton classes
obj = Object.new
def obj.say_hi
puts "Hi! I'm #{self}."
end
obj.say_hi
p obj.methods(false)
p obj.private_methods(false)
@cqfd
cqfd / learn-c-with-gdb.org
Created August 17, 2012 00:02
Learning c with gdb

High-level

I want to write about why GDB is a great tool for learning C. At least part of the difficulty in learning C is that the language isn’t as interactive as using Python or Ruby.

TL;DR You can kinda use gdb as a repl for c

What’s the smallest possible program we could debug to learn about pointers?

@cqfd
cqfd / gist:3387797
Created August 18, 2012 15:40
brew install gdb barf
/usr/bin/clang -Os -w -pipe -march=native -Qunused-arguments -mmacosx-version-min=10.7 -L/usr/local/Cellar/readline/6.2.4/lib -L/usr/local/lib \
-o gdb gdb.o libgdb.a \
-lreadline ../opcodes/libopcodes.a ../bfd/libbfd.a ./../intl/libintl.a -liconv ../libiberty/libiberty.a ../libdecnumber/libdecnumber.a -lncurses -lz -lm -liconv -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -ldl -framework CoreFoundation -lpython2.7 -u _PyMac_Error /System/Library/Frameworks/Python.framework/Versions/2.7/Python -lexpat ../libiberty/libiberty.a gnulib/libgnu.a
/usr/bin/clang -Os -w -pipe -march=native -Qunused-arguments -mmacosx-version-min=10.7 -L/usr/local/Cellar/readline/6.2.4/lib -L/usr/local/lib \
-o gdbtui tui-main.o libgdb.a \
-lreadline ../opcodes/libopcodes.a ../bfd/libbfd.a ./../intl/libintl.a -liconv ../libiberty/libiberty.a ../libdecnumber/libdecnumber.a -lncurses -lz -lm -liconv -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2
@cqfd
cqfd / irc_parser.erl
Created September 6, 2012 16:00
Erlang IRC protocol parser
-module(irc_parser).
-export([parse/1]).
% Start by trying to parse a prefix.
parse(":" ++ Msg) ->
case string:chr(Msg, $ ) of
0 ->
error;
PrefixEnd ->
Prefix = string:substr(Msg, 1, PrefixEnd - 1),
@cqfd
cqfd / python_classmethods.py
Created October 3, 2012 19:30
Python class methods
class A(object):
@classmethod
def make_an_instance(cls):
return cls()
a = A.make_an_instance()
@cqfd
cqfd / gist:4049153
Created November 10, 2012 00:03
Blog post on unfolding the BitTorrent peer protocol

i'm doing a bittorrent client in erlang

task: parse a lump of raw binary data from a socket into a list of bittorrent messages

my original implementation worked but was messy; the logic for parsing individual bittorrent messages was tangled with the logic for accumulating them into a list [should show this, ought to be somewhere in github.com/happy4crazy/ebc]

@cqfd
cqfd / gist:4090719
Created November 16, 2012 20:42
Another unfold blog draft

Over the past few months at Hacker School, I've been learning Erlang. Erlang is a fun choice for writing networked applications, and I've decided to try implementing a BitTorrent client as a way to challenge my developing Erlang skills.

BitTorrent peers communicate over TCP sockets using a binary protocol. This means that in order to implement a BitTorrent client, you'll need to write some code to read a lump of binary data off a socket and then parse it into a list of BitTorrent messages.

@cqfd
cqfd / gist:4150368
Created November 26, 2012 20:20
Python sockets + threads
import socket
import threading
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
lsock.bind(('0.0.0.0', 4567))
lsock.listen(1)
conn, addr = lsock.accept()
@cqfd
cqfd / gist:4165234
Created November 28, 2012 22:37
Backbone pseudo code
ContainerView = Backbone.View.extend({
initialize: function() {
this.twitterView = ....;
this.twitterView.on("new-results", this.handleNewResults, this);
this.gameView = ....;
},
render: function() {
this.$el.empty();
...
},