Skip to content

Instantly share code, notes, and snippets.

View willtim's full-sized avatar

Tim Williams willtim

  • London
View GitHub Profile
@willtim
willtim / GreenThreads.scala
Created August 16, 2012 16:42
An example of lightweight (Green) application threads using the continuation monad
/**
* An example of lightweight (Green) application threads using the continuation monad,
* which gives us scalable non-blocking IO without nested callbacks.
* @author willtim
*/
object GreenThreads {
// a thread (continuation) represents the rest of the computation
sealed abstract class Thread
case class Print(str: String, rest: Thread) extends Thread
@willtim
willtim / Process.hs
Created December 13, 2012 21:30
A Bot monad which provides a reader environment with suspended steps yielding Command values
module Process where
import Control.Monad.Reader
import Control.Monad.Cont
-- | this is the infinite stream of steps from the Bot, each step takes an environment
-- and yields a command and the entire rest of the computation
type Process = Reader DashBoard Step
data Step = Step { stepCmd :: Command, stepNext :: Process }
@willtim
willtim / Netwire.hs
Created November 22, 2013 08:29
Hoodlums experiments with Netwire and SDL. Requires: netwire, sdl and sdl-image
import Prelude hiding ((.), id)
import Control.Wire
import Control.Arrow
import Data.Set (Set)
import Data.Monoid
import qualified Data.Set as S
import qualified Graphics.UI.SDL as SDL
import qualified Graphics.UI.SDL.Image as SDLi
@willtim
willtim / memoFix.hs
Created November 29, 2013 18:33
MemoFix Example
import Control.Concurrent.MVar
import Control.Monad.Fix (mfix)
import qualified Data.Map as M
fix :: (t -> t) -> t
fix f = f (fix f)
fib :: (Eq a, Num a) => (a -> a) -> a -> a
fib _ 0 = 0
fib _ 1 = 1
@willtim
willtim / SplitOn.hs
Created April 28, 2014 19:41
Split a string up by occurrences of a substring
import Control.Arrow (second)
import qualified Data.List as L
splitOn :: Eq a => [a] -> [a] -> [[a]]
splitOn sep = L.unfoldr f
where
f [] = Nothing
f xs = Just $ second (drop l) $ breakList (L.isPrefixOf sep) xs
l = length sep
@willtim
willtim / Compiler1.hs
Created May 6, 2014 16:54
Compiler for a stack machine
{-# LANGUAGE NoMonomorphismRestriction #-}
import Control.Applicative
import Text.ParserCombinators.UU (pChainl)
import Text.ParserCombinators.UU.BasicInstances(pSym)
import Text.ParserCombinators.UU.Utils
-- | Huttons razor
data Exp = Add Exp Exp
@willtim
willtim / Compiler2.hs
Last active August 29, 2015 14:01
Compiler for an SSA register machine
{-# LANGUAGE NoMonomorphismRestriction #-}
import Control.Applicative
import Control.Monad.State
import Control.Monad.Writer
import Data.IntMap (IntMap)
import qualified Data.IntMap as IM
import Text.ParserCombinators.UU (pChainl)
import Text.ParserCombinators.UU.BasicInstances(pSym)
import Text.ParserCombinators.UU.Utils
@willtim
willtim / source-annotations.tex
Created June 12, 2014 06:23
Source annotation example in Latex
\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{fit,calc,shadows}
% Define styles for balloons and lines
\tikzstyle{line} = [draw, rounded corners=3pt, -latex]
\tikzstyle{balloon} = [draw, fill=blue!20, opacity=0.4, inner sep=4pt, rounded corners=2pt]
@willtim
willtim / Prac1_handleKey.hs
Last active August 29, 2015 14:03
Hoodlums: Purely Functional Text Editing
------------------------------------------------------------
-- Hoodlums: Purely Functional Text Editing
--
-- handleKey implementation for Prac1.hs
-- see https://personal.cis.strath.ac.uk/conor.mcbride/CS410/
--
-- Note: you will also need to change the "Here" data type to
-- accomodate a String payload
--
@willtim
willtim / .tmux.conf
Created January 13, 2017 18:34
tmux.conf for cygwin
# Tim Williams .tmux.conf
# Note: can query default bindings with tmux list-keys
# Hierarchy:
# Server
# ㄴSession
# ㄴWindow
# ㄴPane