Skip to content

Instantly share code, notes, and snippets.

View begriffs's full-sized avatar

Joe Nelson begriffs

View GitHub Profile
@begriffs
begriffs / closure-loop.py
Created January 11, 2012 19:25
Closures in Python bind the same as those in JavaScript
def magic():
x = 42
ret = []
for i in range(3):
ret.append(lambda: x)
x += 1
return ret
fs = magic()
print fs[0]()
@begriffs
begriffs / WithLogging.php
Created January 22, 2012 16:56
Aspect-Oriented PHP logger
<?php
// This class impersonates other objects and logs what they do.
class WithLogging {
// The parameter customMessage is a function that knows something about
// callee and can give extra interesting information about its state.
function __construct($callee, $customMessage = NULL) {
$this->callee = $callee;
$this->customMessage = $customMessage ?: function($obj) { return ''; };
}
@begriffs
begriffs / word.rb
Created January 29, 2012 17:47
English words for numbers
def word(n)
divisors = {
:vigintillion => 10**63, :novemdecillion => 10**60,
:octodecillion => 10**57, :septendecillion => 10**54,
:sexdecillion => 10**51, :quindecillion => 10**48,
:quattuordecillion => 10**45, :tredecillion => 10**42,
:duodecillion => 10**39, :undecillion => 10**36,
:decillion => 10**33, :nonillion => 10**30,
:octillion => 10**27, :septillion => 10**24,
:sextillion => 10**21, :quintillion => 10**18,
@begriffs
begriffs / pascal.rb
Created February 4, 2012 04:19
Calculate values for cells in Pascal's triangle
def pascal(row, pos)
throw :badArguments if pos > row or pos < 0 or row < 0
def fact(n); 1.upto(n).inject(:*) || 1; end
fact(row) / (fact(pos) * fact(row - pos))
end
@begriffs
begriffs / partitions.py
Created March 2, 2012 03:29
generate all set partitions
def partitions(set_):
if not set_:
yield []
return
for i in xrange(2**len(set_)/2):
parts = [set(), set()]
for item in set_:
parts[i&1].add(item)
i >>= 1
for b in partitions(parts[1]):
@begriffs
begriffs / etym.rb
Created March 15, 2012 13:48
Learn word etymologies quickly from the command line
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'colorful'
url = "http://www.etymonline.com/index.php?search=#{ARGV[0]}"
doc = Nokogiri::HTML(open(url))
doc.css('dl').search('dt').each do |node|
puts "#{node.text}".white
@begriffs
begriffs / cycles.py
Created March 27, 2012 02:25
Decompose a permutation into disjoint cycles
def cycles(perm):
remain = set(perm)
result = []
while len(remain) > 0:
n = remain.pop()
cycle = [n]
while True:
n = perm[n]
if n not in remain:
break
@begriffs
begriffs / etym
Created May 14, 2012 15:15
Etymology Lookup
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'colorful'
url = "http://www.etymonline.com/index.php?search=#{ARGV[0]}"
doc = Nokogiri::HTML(open(url))
doc.css('dl').search('dt').each do |node|
puts "#{node.text}".white
@begriffs
begriffs / gist:3505211
Created August 28, 2012 23:15
vcr rspec
$ rake spec
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
1) Excon hook when Excon's streaming API is used properly records and plays back the response
Failure/Error: recorded.should eq(played_back)
expected: ""
got: "FOO!"
@begriffs
begriffs / relax.rb
Created October 7, 2012 21:54
Generate Relax Ng template from HTML
require 'nokogiri'
require 'open-uri'
unless [1,2].include? ARGV.length
puts "Usage: arrest template-url [css-selector]"
exit 1
end
def relax node, indent = 0
tab = ' '