Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
int fib(n) {
if ((n == 0)||(n == 1)) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
(define (compile io-port)
(display "(display \"")
(let loop ((c (read-char io-port))
(compile-state "normal"))
(if (not (eof-object? c))
(begin
(loop (read-char io-port) (handle-token c compile-state)))
(if (equal? compile-state "normal")
(begin
(display "\")")
@rsayers
rsayers / gist:935901
Created April 22, 2011 02:35
Dynamic Programming
(define lambda-cache (make-hash-table))
(define (add-to-cache args)
(let ((fn (car args))
(a (cadr args)))
(hash-table-set! lambda-cache args (fn a))
(hash-table-ref lambda-cache args)))
(define (dynamic-call . args)
@rsayers
rsayers / watchmaker.rb
Created December 16, 2011 00:12
String evolution as described in Richard Dawkins: "The Blind Watchmaker"
# from: http://www.informit.com/articles/article.aspx?p=683059&seqNum=36
class String
def levenshtein(other, ins=2, del=2, sub=1)
# ins, del, sub are weighted costs
return nil if self.nil?
return nil if other.nil?
dm = [] # distance matrix
# Initialize first row values
dm[0] = (0..self.length).collect { |i| i * ins }
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
#include <chibi/eval.h>
static void *callback(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info) {
@rsayers
rsayers / lit.rb
Created September 6, 2012 17:47
Literate programming in Ruby
def lit(src)
eval src.split("\n").delete_if { |l| l[0]!='>' }.map {|l| l[1,l.length] }.join("\n")
end
lit <<src
This is a big wall of text with no Ruby code...
Does this work?
1 1 + .
I bet it didn't... maybe the following works:
@rsayers
rsayers / hiragana.rb
Created June 24, 2013 22:25
Simple console based hiragana drill tool
# -*- coding: utf-8 -*-
pairs = [["あ","a"],
["か","ka"],
["さ","sa"],
["た","ta"],
["な","na"],
["は","ha"],
["ま","ma"],
["や","ya"],
@rsayers
rsayers / spam.sh
Created July 18, 2013 17:14
I have a vps that does nothing but handle mail for 1 account, I needed something to block spam, but running spamd was too heavy for my vps. I run this every minute. it simply finds every mail <2 minutes old and moves it if spam assassin claims it is spam. It's low tech, but also low requirements.
#!/bin/sh
INBOX="/path/to/Maildir/cur"
SPAM="/path/to/Maildir/.spam/cur"
cd $INBOX
for i in `find ./ -type f -mmin -2`
do
echo "Testing $i"
if (cat $i | spamassassin | grep 'X-Spam-Flag..YES')
then echo "$i is spam"
mv $i $SPAM
@rsayers
rsayers / gist:8408198
Created January 13, 2014 21:09
Rough implementation of Base64 done to understand it a bit better
# This doesnt handle padding or non ascii, but its a rough implementation based on the wikipedia description
module RobBase64
def self.encodebyte(byte)
case byte
when 0..25 then (byte+65).chr
when 26..51 then (byte-26+97).chr
when 52..61 then (byte-4).chr
when 62 then "+"
when 63 then "/"
@rsayers
rsayers / gist:8409139
Created January 13, 2014 22:12
Base256 encoding, again from the wikipedia example
# -*- coding: utf-8 -*-
class Base256
def initialize
@list = [['aardvark','adroitness'],['absurd','adviser'],['accrue','aftermath'],['acme','aggregate'],['adrift','alkali'],['adult','almighty'],['afflict','amulet'],['ahead','amusement'],['aimless','antenna'],['Algol','applicant'],['allow','Apollo'],['alone','armistice'],['ammo','article'],['ancient','asteroid'],['apple','Atlantic'],['artist','atmosphere'],['assume','autopsy'],['Athens','Babylon'],['atlas','backwater'],['Aztec','barbecue'],['baboon','belowground'],['backfield','bifocals'],['backward','bodyguard'],['banjo','bookseller'],['beaming','borderline'],['bedlamp','bottomless'],['beehive','Bradbury'],['beeswax','bravado'],['befriend','Brazilian'],['Belfast','breakaway'],['berserk','Burlington'],['billiard','businessman'],['bison','butterfat'],['blackjack','Camelot'],['blockade','candidate'],['blowtorch','cannonball'],['bluebird','Capricorn'],['bombast','caravan'],['bookshelf','caretaker'],['brackish','celebrate'],['breadline','cellulose'],['br