Skip to content

Instantly share code, notes, and snippets.

View forestbelton's full-sized avatar

Forest Belton forestbelton

View GitHub Profile
@forestbelton
forestbelton / nsify.js
Created February 24, 2013 04:11
Prepends class / id selectors with "ns_" if data-namespace="ns" is given on the <link> tag.
(function() {
var nsify = function(ns, selChunk) {
sels = selChunk.replace(/ /g, '').split(',');
for(var i = 0; i < sels.length; ++i)
if(sels[i][0].match(/[.#]/))
sels[i] = sels[i][0] + ns + '-' + sels[i].substring(1);
return sels.join(',');
@forestbelton
forestbelton / CNF.hs
Created February 24, 2013 04:18
A naive SAT solver in Haskell
import qualified Data.Set as Set
data Clause = Value Bool | Not Int | Literal Int | Or Clause Clause deriving (Eq,Ord)
type CNF = Set.Set Clause
instance Show Clause where
show (Value True) = "T"
show (Value False) = "F"
show (Not x) = "~" ++ show x
show (Literal x) = show x
@forestbelton
forestbelton / Env.pm
Last active June 26, 2021 10:49
A basic LISP interpreter in Perl
#!/usr/bin/env perl
use strict;
use warnings;
package Env;
sub new {
my $class = shift;
my $names = shift;
@forestbelton
forestbelton / gswap.c
Created April 6, 2013 03:31
generic swapping function in C
void gswap(void *v1, void *v2, size_t sz) {
char *c1 = v1;
char *c2 = v2;
char tmp;
while(sz--) {
tmp = *c1;
*c1++ = *c2;
*c2++ = tmp;
}
@forestbelton
forestbelton / qmalloc.c
Created April 6, 2013 03:32
malloc wrapper that tracks size of allocated data
#define find_chunk(ptr) (struct mchunk*)((char*)(ptr) - offsetof(struct mchunk, data))
struct mchunk {
size_t sz;
char data[];
};
void *qmalloc(size_t sz) {
struct mchunk *chunk = malloc(sizeof *chunk + sz);
@forestbelton
forestbelton / strsplit.c
Created April 6, 2013 03:32
string splitting in C (a la PHP's explode())
char **strsplit(const char *s, int delim) {
int state;
char *sout;
char **out;
size_t i, count, word;
/* Skip any leading delimiters. */
while(*s == delim)
++s;
@forestbelton
forestbelton / RPN.hs
Created April 6, 2013 03:33
RPN calculator in Haskell
import Control.Monad
import Data.Char
import Data.List
op f (y:x:xs) = (f x y):xs -- Pop two, evaluate, push.
parse [value] [] = value -- Done parsing, evaluate.
parse _any [] = error "wrong stack size" -- This shouldn't happen!
parse stack (' ':xs) = parse stack xs -- Ignore spaces.
parse stack ('+':xs) = parse (op (+) stack) xs -- Addition.
@forestbelton
forestbelton / bandcamp.py
Created April 6, 2013 03:34
Bandcamp album ripper
from ID3 import *
import os, re, string, urllib2
embed_data = re.compile(r'var EmbedData = {.*?album_title : "([^"]+?)".*?artist : "([^"]+?)".*?};', re.DOTALL)
track_data = re.compile(r'trackinfo : \[([^\]]+?)\]', re.DOTALL)
def grab(url):
f = urllib2.urlopen(url)
data = f.read()
@forestbelton
forestbelton / fplot.py
Created April 6, 2013 03:34
Basic function plotter
import pygame, sys
from pygame.locals import *
from math import floor
import math
WIDTH, HEIGHT = 320, 200
pygame.init()
pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Function plotter')
@forestbelton
forestbelton / snake.coffee
Created April 6, 2013 03:41
A simple snake game
# Copyright (C) 2011 Forest Belton
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in