Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / Strategy.scala
Last active January 3, 2016 12:59
Playing abstract strategy games in general
/** Game rules for state space S and players in P */
trait GameRules[S, P] {
/** Score a position relative to a player. That is, a better
position should have a bigger score than a worse one. */
def children(position: S): List[S]
def turn(position: S): P // Whose turn is it on some position?
}
/** Scores positions for a Strategy. R should have a total ordering (see the
* signature of pickMove below) and should be positive if the given player has
@nvanderw
nvanderw / Config.java
Created January 6, 2014 03:13
lol java
interface MaybeVisitor<A, R> {
public R just(A v);
public R nothing();
}
interface Maybe<A> {
public <R> R fold(MaybeVisitor<A, R> visitor);
}
interface PairVisitor<A, B, R> {
@nvanderw
nvanderw / Free.hs
Created November 22, 2013 01:38
Learning some free monads
{-# LANGUAGE KindSignatures, GADTs #-}
-- Inspired by the Well-Typed "Monads for Free!" slides, available online
data Free :: (* -> *) -> * -> * where
Return :: a -> Free f a
Wrap :: f (Free f a) -> Free f a
instance Functor f => Functor (Free f) where
fmap f (Return x) = Return (f x)
@nvanderw
nvanderw / Turing.hs
Created October 4, 2013 06:39
Classy Turing machines
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, ScopedTypeVariables #-}
module TuringMachine where
import Control.Monad.State.Lazy
import Data.Array.ST
import Data.Array
import Data.Default
import Control.Monad.ST
import Control.Arrow ((&&&))
@nvanderw
nvanderw / gist:6724570
Last active December 24, 2015 01:29
Statically-typed stack effects
module Stack where
import Control.Arrow
class List a
-- Use pairs to represent a cons list which can be statically checked
instance List ()
instance List b => List (a, b)
@nvanderw
nvanderw / Emitter.hs
Created August 25, 2013 21:36
Simple monadic x86 generation using Haskell
import Data.Word
import Data.List
import Control.Monad.State.Strict
import Control.DeepSeq
data EmitterState = EmitterState {
emNextLabel :: Int
} deriving (Read, Show, Eq, Ord)
instance NFData EmitterState where
@nvanderw
nvanderw / why.rb
Created May 26, 2013 17:39
Why is Ruby?
# why do blocks even exist?
def fibs1
a, b = 0, 1
while b < 1000
yield b
a, b = b, a + b
end
end
# it seems like "yield" is just syntax for "invoke this weird callback"
@nvanderw
nvanderw / zalgo.py
Created April 25, 2013 21:52
Zalgo
from __future__ import division
import sys
from random import SystemRandom
# A list of unicode combining characters
COMBINING = [unichr(c) for c in xrange(0x300, 0x370)]
ZalgoError = KeyError