Skip to content

Instantly share code, notes, and snippets.

@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)
@nvanderw
nvanderw / gist:4748706
Created February 10, 2013 07:03
Some fun with Peano numbers
(* Type of natural numbers *)
type nat = Zero
| Succ of nat ;;
exception Undefined ;;
type comparison = LT
| EQ
| GT ;;
@nvanderw
nvanderw / pmf.hs
Last active December 15, 2015 13:39
Discrete random variables in Haskell
import Data.Either
import System.Random
-- |An association list mapping random variable outputs to their
-- probabilities, which should add up to 1.
type ProbMass a = [(a, Double)]
-- |Given a uniform random number in [0, 1) and a probability mass function
-- (PMF) describing the probabilities of various outcomes of a random
-- variable X, give a random outcome.
public class Thunks {
/**
* Interface which represents unary functions. Can be anonymously
* instantiated to simulate lambdas.
*/
public static interface Fn<A, B> {
public B call(A arg);
}
/**
@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
@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 / 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 / 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 / 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 ((&&&))