Skip to content

Instantly share code, notes, and snippets.

View willtim's full-sized avatar

Tim Williams willtim

  • London
View GitHub Profile
@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 / 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 / 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 / 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 / WordCount.scala
Created August 16, 2012 16:28
Purely functional version of Kernighan and Ritchie’s wc program
/**
* @author willtim
* Purely functional version of Kernighan and Ritchie’s wc program
*
* #include <stdio.h>
* #define IN 1 /* inside a word */
* #define OUT 0 /* outside a word */
* int blank(int c) {
* return ( c==’ ’ || c==’\n’ || c==’\t’);
* }
@willtim
willtim / Dataflow.scala
Created August 16, 2012 16:25
A purely functional dataflow graph using Arrows
/**
* A purely functional dataflow graph using Arrows.
* Note that the groupBy compbinator is built from the accum primitive.
* @author willtim
*/
object Dataflow {
case class Auto[A,B] (run: A => (B, Auto[A,B])) { f =>
def >>> [C] (g: Auto[B,C]): Auto[A,C] =
@willtim
willtim / Coded.hs
Created March 20, 2012 22:08
Cracking codes using backtracking
import Control.Applicative
import Control.Monad
import Control.Monad.State
import Control.Monad.Logic
import Data.List
import qualified Data.Map as M
type BindingT k v = StateT (M.Map k v)
bind :: (MonadPlus m, Ord k, Eq v) => k -> v -> BindingT k v m ()
@willtim
willtim / indexing.clj
Created January 21, 2012 00:29
Building a book index
(use '[clojure.core.match :only [match]])
;; quick and simple trie implementation from the description
;; in the book "Purely Functional Data Structures" by Okasaki
(def empty-trie [:trie :value nil :edges {}])
(defn lookup [key trie]
(match [key trie]
[[] [:trie :value nil :edges _]] nil
@willtim
willtim / IterateesHomework.hs
Created December 6, 2011 09:27
Haskell Iteratees Example
module IterateesHomework where
import Prelude hiding (words, unwords, init)
import Data.Text hiding (reverse, map, take)
import Data.Enumerator hiding (map)
import Control.Applicative ((<$>))
import Data.List (sort)
import qualified Data.Enumerator.Binary as EB
import qualified Data.Enumerator.Text as ET
import qualified Data.Enumerator.List as EL
@willtim
willtim / clj_vtd_xml.clj
Created February 11, 2011 18:15
Simple Clojure API for VTD-XML - much faster than clojure.contrib.zip-filter.xml
(ns willtim.clj-vtd-xml
(:import [com.ximpleware VTDGen VTDNav AutoPilot])
(:require
[clojure.contrib.duck-streams :as ds]))
;;
;; Clojure API for VTD-XML
;;
;; Designed to work like clojure.contrib.zip-filter.xml, e.g.
;;