Skip to content

Instantly share code, notes, and snippets.

View willtim's full-sized avatar

Tim Williams willtim

  • London
View GitHub Profile
module Expressions where
import qualified Data.Map as M
import Text.Printf
import Control.Monad
import qualified Control.Monad.Error as E
testExpr = (Op Sum (Val 1) (Symbol "x"))
type Value = Either Error Int
@willtim
willtim / Parser.hs
Created November 2, 2010 22:48
Applicative Parsing
import Data.Maybe
import Text.Printf
import Control.Applicative
data Expr = Term Int | Op Operator Expr Expr | Var String
deriving (Show, Eq)
data Operator = Sum | Mult | Sub | Div
deriving (Show, Eq)
@willtim
willtim / query_comprehension.clj
Created January 25, 2011 10:52
Clojure Query Comprehension Macro
(ns query-comprehension
(:use clojure.contrib.prxml))
;;
;; Enhanced List Comprehensions supporting arbitary group-by, filtering and ordering.
;;
;; (from [ BINDING GENERATOR -- One or more Generator clauses
;; :when PREDICATE ] -- Zero or more Guards
;; :group-by [ EXPRESSION :into [KEY_NAME GROUP_NAME]] -- Optional Group-By qualifier
;; :having PREDICATE -- Optional Group Guard
@willtim
willtim / gist:797604
Created January 26, 2011 22:18
Google Code Jam C - ThemePark
module Main where
import Text.Printf
import Data.List
profit :: Int -> Int -> [Int] -> Int
profit cap rides groups
| sum groups <= cap = rides * sum groups
| otherwise =
case toCaps . findCycle $ rideCaps cap rides groups of
@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.
;;
@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 / 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 / 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 / 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 / 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’);
* }