Skip to content

Instantly share code, notes, and snippets.

@rmg
rmg / Makefile
Created February 24, 2010 22:10
A rope-like approach to building up a DOM and spitting it out as text in C
# A rope-like HTML/SGML/XML tree generator
# Initializers inside "for (...)" is technically only as of C99
CFLAGS = --std=c99
# If we can live with the trailing 0's in the function calls, go pedantic
ifdef PEDANTIC
CFLAGS += -pedantic -Werror -DPEDANTIC
endif
@rmg
rmg / retry.py
Created February 24, 2010 22:32
Little retry loop wrappers (handling return codes and exceptions)
from functools import wraps
def retry(attempts, f):
"""
Returns a callable that will invoke @f() and return result, suppressing
exceptions up to @attempts times
"""
@wraps(f)
def wrapper(*args, **kwds):
hardfail = None
@rmg
rmg / README
Created August 22, 2011 02:16
Base class extensions to allow versioning and logging of operations on base types.
http://code.google.com/p/google-diff-match-patch/
http://stackoverflow.com/questions/4599456/textually-diffing-json/4599500#4599500
http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes
@rmg
rmg / irb session
Created December 5, 2012 23:08
Sin < ActiveRecord::Base
[75] pry(main)> def_memoized(:fac) {|x| x==0?1:x*fac(x-1) }
=> #<Proc:0x000000062147a0@/home/rgraham/working/cmportal/app/models/memoized_result.rb:7 (lambda)>
[76] pry(main)> fac(5)
MemoizedResult Load (0.6ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(5)' LIMIT 1
MemoizedResult Load (0.7ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(4)' LIMIT 1
MemoizedResult Load (0.5ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(3)' LIMIT 1
MemoizedResult Load (0.3ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(2)' LIMIT 1
MemoizedResult Load (0.2ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(1)' LIMIT 1
MemoizedResult Load (0.2ms) SELECT `memoized_results`.* FROM `memoized_results` WHERE `memoized_results`.`key` = 'Object#fac(0)' LIMIT 1
@rmg
rmg / adapter_test.rb
Last active December 18, 2015 20:58 — forked from jodosha/adapter_test.rb
require 'test_helper'
shared_examples_for 'An Adapter' do
describe '#read' do
before do
@adapter.write(@key = 'whiskey', @value = "Jameson's")
end
it 'reads a given key' do
@adapter.read(@key).must_equal(@value)
@rmg
rmg / lineage.rb
Last active December 19, 2015 12:18
Lineage: Tap into the latent enumerable powers of your class's directed graph!
require 'set'
def Lineage(enum_name = :lineage, parent_accessor = :parent)
Module.new do
define_method enum_name do |&block|
if block.nil?
enum_for(enum_name)
else
next_parent, visited = self, Set[self]
while next_parent = next_parent.send(parent_accessor)
@rmg
rmg / Makefile
Last active December 19, 2015 19:59
FP-inspired C
CFLAGS += --std=c11 -Wall -Wextra -Winline -finline-functions
CFLAGS += -O3
CFLAGS += -save-temps
# CFLAGS += -g
#LDFLAGS += -flto
run_tests: tests
./tests
$(wildcard *.o): list.h
@rmg
rmg / renderable_file.rb
Last active December 25, 2015 17:49
Example of how to use ERB and DATA in Ruby. Handy combination.
require 'ostruct'
require 'erb'
class RenderableFile
def self.get_data(name = nil)
unless const_defined?(:DATA)
const_set(:DATA, File.open( __FILE__ ).read.split( /^__END__\n/, 2 ).last)
end
if name
DATA.split( /^#{name}\n/, 2 ).last
@rmg
rmg / domain-test.js
Last active January 2, 2016 13:19
Domain double-pop
var domain = require('domain')
, d1 = new domain.Domain()
, d2 = new domain.Domain()
, d3 = new domain.Domain()
, d4 = new domain.Domain()
console.log("Starting: ", domain._stack)
d1.enter()
console.log("Entered d1: ", domain._stack)
d2.enter()
@rmg
rmg / podcasts.md
Created January 14, 2014 20:13
Podcast Reviews

podcasts I really enjoy:

  • Mostly Erlang (most episodes aren't actually about Erlang, main theme seems to be functional programming)
  • Ruby Rogues (mostly Ruby, but I'd say more accurately about programming in general by really smart people who happen to use Ruby)
  • Wideteams (working remotely, found this because the host is a Ruby Rogue - I (heart) Avdi Grimm)
  • NodeUp (actually node specific, so probably not as useful to non-node people, dshaw's voice may take getting used to)
  • Hanselminutes (Scott Hanselman is a MS developer, but the podcast isn't about that - basically him interviewing people that he and other developers find interesting)
  • This Developer's Life (about deverlopers the people, not their jobs, these are not generally discussions, they're more like stories.. awesomely put together)
  • Coder Radio ("the art and business of software development", good discussions with audience, who seem to mostly be beginners)
  • Giant Robots Smashing Into Other Giant Robots (good interviews with many differen