Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
chris-taylor / IOAction.hs
Last active September 28, 2020 12:32
Code for my blog post about pure I/O
data IOAction a = Return a
| Put String (IOAction a)
| Get (String -> IOAction a)
get = Get Return
put s = Put s (Return ())
seqio :: IOAction a -> (a -> IOAction b) -> IOAction b
seqio (Return a) f = f a
seqio (Put s io) f = Put s (seqio io f)
@chris-taylor
chris-taylor / common.txt
Last active December 11, 2015 09:08
Quick script for counting up most common words in a text, ignoring very common words
the
be
am
are
is
was
were
been
to
of
@chris-taylor
chris-taylor / em.matlab
Created December 5, 2012 10:07
Expectation Maximization
function em(X,theta)
% Expectation maximization, P coins
%
% X is TxN matrix of coin flip results (1 = heads, 0 = tails)
% theta is 1xP vector of probabilities (0 < theta < 1)
% Convergence criterion (relative difference)
tol = 1e-6;
% Compute parameters of distribution assigning coins to outputs
@chris-taylor
chris-taylor / US2012ElectionPredictions.txt
Created November 4, 2012 18:00
US 2012 Election predictions
State Winner Confidence
California DEM 100%
Connecticut DEM 100%
District of Columbia DEM 100%
Massachusetts DEM 100%
New York DEM 100%
Vermont DEM 100%
Maryland DEM 100%
Washington DEM 100%
Hawaii DEM 100%
@chris-taylor
chris-taylor / Central Limit Theorem Demo
Created September 27, 2012 10:12
centrallimittheorem.octave
%Uniform distribution
x1 = randi(10,1000,1);
[n1 xout1] = hist(x1);
%Sum of uniform distribution
x2 = sum(randi(10,1000,12),2);
m = mean(x2);
s = std(x2);
[n2 xout2] = hist(x2);
@chris-taylor
chris-taylor / simCancer.m
Created June 22, 2012 15:56
Simulation of cancer cells
function [plate cellLocs] = simCancer(sz,pDiv,maxCells)
directions = [1 0; -1 0; 0 1; 0 -1]; % Possible grid directions
plate = zeros(sz); % Initial state of the plate
cellLocs = zeros(sz*sz,2); % (x,y) location of cells
numCells = 1; % Initial number of cells
generation = 1; % Initial generation
@chris-taylor
chris-taylor / unfold.hs
Created June 14, 2012 23:05
Generic unfolds (partial solution)
{-# LANGUAGE ViewPatterns, GADTs, ScopedTypeVariables #-}
-- Functions to fold and unfold. We are using ViewPatterns to make the symmetry between fold and unfold explicity.
foldr2 :: (Either (a,b) () -> b) -> [a] -> b
foldr2 f [] = f $ Right ()
foldr2 f (x:xs) = f $ Left (x, foldr2 f xs)
unfoldr2 :: (b -> Either (a,b) ()) -> b -> [a]
unfoldr2 f (f -> Right () ) = []
@chris-taylor
chris-taylor / Module1.hs
Created June 13, 2012 13:44
Reader monad example
module Module1 where
import Control.Monad.Reader
data Config = Config { arg :: Int }
initialize :: IO Config
initialize = do
n <- getLine
return $ Config (read n)
@chris-taylor
chris-taylor / evolution.m
Created June 12, 2012 13:02
Relative entropy as applied to an evolutionary competition between two species
function [T Y] = relent(Tspan)
%% Configuration
leg = {'Species 1','Species 2'};
opts = odeset();
opts.AbsTol = 1e-8;
opts.RelTol = 1e-4;
@chris-taylor
chris-taylor / jsmath.js
Created June 11, 2012 21:41
jsMath inclusion
/**********************************************************************
*
* Customize the values given below to suit your needs.
* You can make additional copies of this file with
* different customizated settings if you need to load
* jsMath with different parameters.
*
* Load this page via:
*
* <SCRIPT SRC="path-to-jsMath/easy/load.js"></SCRIPT>