Skip to content

Instantly share code, notes, and snippets.

View dpk's full-sized avatar

Daphne Preston-Kendal dpk

View GitHub Profile
@dpk
dpk / define-macro.scm
Created March 4, 2012 23:57
define-macro for Chicken Scheme
; kind of a stupid implementation but with
(define-syntax define-macro
(er-macro-transformer
(lambda (exp rename compare)
(if (symbol? (cadr exp))
(let ((name (cadr exp))
(expndr (caddr exp))
(-exp (gensym)) (-rename (gensym)) (-compare (gensym)))
`(define-syntax ,name
@dpk
dpk / ack.scm
Created May 28, 2012 17:51
Braindump No. 1: Ackermann Function
(define (ack m n)
(cond ((zero? m) (+ n 1))
((and (> m 0) (zero? n)) (ack (- m 1) 1))
((and (> m 0) (> n 0)) (ack (- m 1) (ack m (- n 1))))))
@dpk
dpk / gist:2830988
Created May 29, 2012 21:47
djb2 and djb2a hash functions in Ruby
def djb2 str
hash = 5381
str.each_byte do |b|
hash = (((hash << 5) + hash) + b) % (2 ** 32)
end
hash
end
def djb2a str
@dpk
dpk / gist:2857091
Created June 2, 2012 07:09
txt2num: convert a 'telephone word' into raw numbers
#!/usr/bin/env ruby
def txt2num str
letters = {'a' => 2, 'b' => 2, 'c' => 2,
'd' => 3, 'e' => 3, 'f' => 3,
'g' => 4, 'h' => 4, 'i' => 4,
'j' => 5, 'k' => 5, 'l' => 5,
'm' => 6, 'n' => 6, 'o' => 6,
'p' => 7, 'q' => 7, 'r' => 7, 's' => 7,
't' => 8, 'u' => 8, 'v' => 8,
@dpk
dpk / gist:2940135
Created June 16, 2012 06:01
num2txt: convert a number into a lot of possible 'telephone words'. pass `-w` to remove all the non-words according to /usr/share/dict/words
#!/usr/bin/env ruby
NUMS = [[], [], ['a', 'b', 'c'], ['d', 'e', 'f'],
['g', 'h', 'i'], ['j', 'k', 'l'], ['m', 'n', 'o'],
['p', 'q', 'r', 's'], ['t', 'u', 'v'], ['w', 'x', 'y', 'z']]
def num2txt str
ps = []
str.each_char do |char|
@dpk
dpk / gist:3056263
Created July 5, 2012 20:33
Current time in location of IP address.
#!/usr/bin/env ruby
require 'geoip'
require 'tzinfo'
geoip = GeoIP.new('GeoLiteCity.dat')
location = geoip.city ARGV[0]
if location.nil?
puts Time.now.utc.to_datetime.iso8601
@dpk
dpk / gist:3808680
Created September 30, 2012 23:04
Tinki, a really short wiki
#!/usr/bin/env ruby
%w{sinatra redcarpet erb}.map{|l|require l};include Redcarpet
md=Markdown.new Class.new(Render::HTML){def preprocess text;text.gsub /\[\[([\w-]+?)\]\]/,'[\1](/\1)';end},filter_html:1
helpers{include ERB::Util};Dir.mkdir'w'unless Dir.exist?'w'
get('/'){redirect to'home'}
get('/:p'){f='w/'+params[:p];File.exist?(f)? "<h1>#{h params[:p]}</h1>#{md.render File.read f}<a href=\"/#{params[:p]}/edit\">edit</a>; last was #{File.mtime f}": redirect(to params[:p]+'/edit')}
get('/:p/edit'){"<h1>Edit #{h params[:p]}</h1><form method=post><textarea name=c>#{h File.read "w/#{params[:p]}" rescue()}</textarea><input type=submit></form>"}
post('/:p/edit'){File.write('w/'+params[:p],params[:c]);redirect to params[:p]}
Ciphertext, base64-encoded:
myitFUAbTS1M4VQTl3P3cWWoJt8xvv9mmIJbrd1vkPy0RLdwrpwp+r5KYUX5
b8cHARXp1xknwstJnxZNr6f0lB1b+J7E+WW/e7rmuHDlkin+pJ/yYNn6s2Dh
YG3AWUs5dYGPFRsC7mpuTc4IJxLo6L/H0UKI8pSCVd0P11pxmshGeuEvFurm
3P0wyTr9kA4QVencMuv48rLz4SEFO4n5iYMkJ8YAgVHmbZzgvpGPrUTWCzPI
9YCfXoqVydFCCRlt6ZHmGUK65S+ZIod/JmrUKzw/zlSsgs2IGIbzuLlujzfC
cN6TUEuwy6rNkdke0ud8j4a9wER52CtnBY1eqohC5Dx5CGsZdg4cs3zrUG4r
LrSRhcdmyNxPrB5IhzezzVcw62nBbXJbQfPDKYq3xj02o5sBka4METYS1E4X
Z+h0yqZy+6XhjX8IsveLQ7m9vNGY8E4+tWWR8bE5Hu7eT8VmjlRU8UjZatz5
@dpk
dpk / gist:3828768
Created October 3, 2012 18:21
don't use more than 100,000 cons pairs
; <sbp> this is why lisp engineers aren't allowed to pilot submarines
(setq *all-conses* (make-array 100000))
(dotimes (ii 100000) (setf (aref *all-conses* ii) (cons nil nil)))
(setq *conses-index* 0)
(defun cons (hd tl)
(let ((the-cons (aref *all-conses* *conses-index*)))
(setq *conses-index* (1+ *conses-index*))
(setf (car the-cons) hd)
@dpk
dpk / gist:3848296
Created October 7, 2012 12:46
Ruby HTML Diff
require 'hpricot'
require 'diff/lcs'
class HTMLDiff
def self.diff a, b
self.new(a, b).compare
end
def initialize a, b