Skip to content

Instantly share code, notes, and snippets.

@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
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 / 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.
@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 / 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 / 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 / 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 / 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 / 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