Skip to content

Instantly share code, notes, and snippets.

@nvanderw
nvanderw / pre-receive
Created June 15, 2012 23:52
A pre-receive hook for blacklisting certain objects
#!/usr/bin/env bash
# Blacklist commit 1abfdd7
root=$(awk '{print $2}')
objects=$(git rev-list --objects $root | awk '{print $1}')
echo $objects | grep 1abfdd7cb8c67ead50757ea596aa0f01b0ab1389 > /dev/null
if [[ $? -eq 0 ]]
then
@nvanderw
nvanderw / gist:2963459
Created June 21, 2012 02:18
Exercise Set 7.1
import System.Environment
sumSquares :: (Integral a) => a -> a
sumSquares n = n*(n+1)*(2*n+1) `div` 6
sumCubes :: (Integral a) => a -> a
sumCubes n = ((n^2 + n) `div` 2)^2
main = do
args <- getArgs
@nvanderw
nvanderw / fibs.hs
Created August 4, 2012 03:08
Polymorphic fibonacci generation using monad transformers
import Control.Monad
import Control.Monad.State.Lazy
-- Simple way to get Fibonacci numbers using monad transformers
fibT :: (Monad m, Num a) => StateT a (StateT a m) ()
fibT = get >>= \x -> lift get >>= \y -> put y >> (lift . put $ x + y)
-- Get the nth Fibonacci number
fib :: Num a => Int -> a
fib n = execState (execStateT (replicateM_ n fibT) 0) 1
@nvanderw
nvanderw / functors.py
Created August 21, 2012 07:19
Typeclass stuff in Python
import itertools
# Monad instance on a Python iterator, very similar to Haskell's list monad.
# In general, Haskell typeclass instances can be regarded as dictionaries which
# map the implemented function to its implementation. For a good explanation of
# this, see Philip Wadler's "Faith, Evolution, and Programming Languages"
# lecture. What this means for us is that we can write a function whose type
# signature in Haskell would be:
# Monad m => f m
#
@nvanderw
nvanderw / Stack.hs
Created August 22, 2012 04:56
Stack-based programming using monad transformers
import Control.Monad
import Control.Monad.Identity (runIdentity)
import Control.Monad.Error
import Control.Monad.State.Lazy
import Data.Maybe (listToMaybe)
-- |Monad transformer which stores a stack internally
type StackT s m = StateT [s] m
@nvanderw
nvanderw / passwords.hs
Created August 30, 2012 18:54
Memorable random words from a dictionary
module Main (main) where
import Control.Monad
import Data.Char
import System.IO
import System.Environment
import System.Random
import System.Exit
@nvanderw
nvanderw / dict.c
Created September 2, 2012 21:13
Some old hash table code
#include "dict.h"
/* Initializes the hash table at table.
* Arguments:
*
* init_size: the initial number of chains in the hash table. If 0, defaults
* to a hardcoded value.
* key_hash: a hashing function that operates on keys
* key_eq: function which returns nonzero iff two keys are equal.
*
@nvanderw
nvanderw / example.py
Created October 18, 2012 01:19
Hacks that make Python more functional
from functional import fun
from itertools import imap, ifilter, takewhile, count
@fun(2)
def add(x,y): return x + y
# Here I define a function which takes a string and shows what it evaluates to.
@fun(1)
def evalPrint(s):
print "%s -> %s" % (s, eval(s))
@nvanderw
nvanderw / p23.rkt
Created November 20, 2012 05:13
Project Euler #23
#lang racket
; Solution to Project Euler problem #23
(require racket/set)
; Predicate to check if m | n
(define (divides? m n)
(= 0 (modulo n m)))
@nvanderw
nvanderw / dispatch.py
Created December 3, 2012 22:09
Decorators for nice left-and-right composition
API_HANDLERS = {}
def register_handler(func, name=None):
if name is None:
name=func.__name__
API_HANDLERS[name] = func
return func
def dispatch(name, request):
return API_HANDLERS[name](request)