Skip to content

Instantly share code, notes, and snippets.

View Ceasar's full-sized avatar

Ceasar Ceasar

View GitHub Profile
@Ceasar
Ceasar / sitemap.py
Created August 12, 2012 22:32
Proof of concept code to solve the problem of how to build a sitemap or file system tree.
import os
from pprint import pprint
MAP = {}
CHILDREN_KEY = 'children'
for path, _, filenames in os.walk('./templates'):
path = path.split("/")[2:]
@Ceasar
Ceasar / ringbuffer.py
Created October 6, 2012 18:52
A Python implement of a ring-buffer. NOTE: Doesn't work (appendleft and append can override each other's value since resizing only appends to end)
class RingBuffer(object):
def __init__(self, items=None):
self._left = 0
self._right = 0
self._length = 0
self._items = []
if items is not None:
for item in items:
self.append(item)
@Ceasar
Ceasar / pre-commit
Created October 12, 2012 02:03
Git hook to make and run nosetests
#!/usr/bin/env python
#-*- mode: python -*-
import sh
sh.make()
sh.nosetests()
@Ceasar
Ceasar / map.c
Created November 12, 2012 03:06
Example of how to implement a higher order function in C.
/*
* Example of higher order functions in C.
*/
#include <stdio.h>
void func ( void (*f)(int, int) ) {
int ctr = 0;
for (ctr; ctr < 5; ctr++) {
(*f)(ctr, ctr);
@Ceasar
Ceasar / parse.hs
Created December 1, 2012 22:17
A basic parser written in Haskell. Written as a learning exercise.
import Data.List
-- Evaluate a postfix expression
evalPostfix :: (Num a, Read a) => String -> a
evalPostfix = head . foldl comb [] . words
where
comb (x:y:ys) "*" = (x * y) : ys
comb (x:y:ys) "+" = (x + y) : ys
comb (x:y:ys) "-" = (y - x) : ys
@Ceasar
Ceasar / join.py
Last active December 10, 2015 11:08
Interface to use a CSV like a SQL table.
'''Crudely joins multiple tables together.'''
import sys
import itertools
import table
def join(tables, name=None):
if name is None:
name = "_".join([table.filename for table in tables])
joined = Table(name, [fieldname for table in tables
@Ceasar
Ceasar / DnC.hs
Created January 3, 2013 00:10
Proof of concept abstraction of divide and conquer. Doesn't really work very well...
-- In Divide and Conquer, we solve a problem recursively, applying three steps at each level of recursion.
dnc :: (a -> [a]) -> (a -> b) -> ([b] -> b) -> a -> b
dnc divide con comb x = case divide x of
[] -> con x
xs -> comb $ map (dnc divide con comb) xs
@Ceasar
Ceasar / Logic.lhs
Created January 3, 2013 03:42
Logical functions.
> import Data.Set as S
> import Data.Map as M
> import Data.Maybe
> import Control.Monad (liftM, liftM2)
> import Test.QuickCheck
A statement is a sentence that is determinately true of determinately false independently of the circumstances in which it is used.
> data Proposition = Implies Proposition Proposition
@Ceasar
Ceasar / partition.py
Created February 8, 2013 15:52
Take a predicate and a list and return a pair of lists: those elements that do and do not satisfy the predicate, respectively.
def partition(pred, items):
"""
Take a predicate and a list and return a pair of lists: those elements
that do and do not satisfy the predicate, respectively.
>>> partition(str.isalpha, "ab1c")
(['a', 'b', 'c'], ['1'])
>>> partition(str.isalpha, [])
([], [])
@Ceasar
Ceasar / crash.js
Created February 15, 2013 16:27
Crashes a page after 10 seconds and uses all of a CPU. Left uncorrected, it can crash multiple pages in Chrome.
$(document).ready(function () {
setTimeout(function(){while(1){};},10000);
});