Skip to content

Instantly share code, notes, and snippets.

@nvanderw
nvanderw / Block.elm
Created February 22, 2014 21:54
Bouncing logo demo
import Signal
import Window
(block_width, block_height) = (100, 100)
speed = 1.5
dimensions = Window.dimensions
width = lift fst dimensions
height = lift snd dimensions
@nvanderw
nvanderw / Free.ml
Created February 14, 2014 04:48
Free monads in OCaml, for great referential transparency
module type Functor = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
end
module type Monad = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val return : 'a -> 'a t
@nvanderw
nvanderw / typeclass.hs
Created January 31, 2014 22:00
Typeclasses as datatypes
-- Standard Prelude definitions
-- data Ordering = LT | EQ | GT
-- class Eq a => Ord a where
-- compare :: a -> a -> Ordering
isOrdered :: Ord a => [a] -> Bool
isOrdered (x:y:xs) = case compare x y of
GT -> False
_ -> isOrdered (y:xs)
isOrdered _ = True
@nvanderw
nvanderw / Strategy.scala
Last active January 3, 2016 12:59
Playing abstract strategy games in general
/** Game rules for state space S and players in P */
trait GameRules[S, P] {
/** Score a position relative to a player. That is, a better
position should have a bigger score than a worse one. */
def children(position: S): List[S]
def turn(position: S): P // Whose turn is it on some position?
}
/** Scores positions for a Strategy. R should have a total ordering (see the
* signature of pickMove below) and should be positive if the given player has
@nvanderw
nvanderw / Config.java
Created January 6, 2014 03:13
lol java
interface MaybeVisitor<A, R> {
public R just(A v);
public R nothing();
}
interface Maybe<A> {
public <R> R fold(MaybeVisitor<A, R> visitor);
}
interface PairVisitor<A, B, R> {
@nvanderw
nvanderw / Free.hs
Created November 22, 2013 01:38
Learning some free monads
{-# LANGUAGE KindSignatures, GADTs #-}
-- Inspired by the Well-Typed "Monads for Free!" slides, available online
data Free :: (* -> *) -> * -> * where
Return :: a -> Free f a
Wrap :: f (Free f a) -> Free f a
instance Functor f => Functor (Free f) where
fmap f (Return x) = Return (f x)
@nvanderw
nvanderw / Monad.ml
Last active October 8, 2019 20:24
IO Monad in OCaml
module type FUNCTOR = sig
type 'v f
val map : ('a -> 'b) -> 'a f -> 'b f
end
module type MONAD = sig
type 'v f
val map : ('a -> 'b) -> 'a f -> 'b f
val pure : 'a -> 'a f
val join : ('a f) f -> 'a f
@nvanderw
nvanderw / Turing.hs
Created October 4, 2013 06:39
Classy Turing machines
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, ScopedTypeVariables #-}
module TuringMachine where
import Control.Monad.State.Lazy
import Data.Array.ST
import Data.Array
import Data.Default
import Control.Monad.ST
import Control.Arrow ((&&&))
@nvanderw
nvanderw / gist:6724570
Last active December 24, 2015 01:29
Statically-typed stack effects
module Stack where
import Control.Arrow
class List a
-- Use pairs to represent a cons list which can be statically checked
instance List ()
instance List b => List (a, b)
@nvanderw
nvanderw / Emitter.hs
Created August 25, 2013 21:36
Simple monadic x86 generation using Haskell
import Data.Word
import Data.List
import Control.Monad.State.Strict
import Control.DeepSeq
data EmitterState = EmitterState {
emNextLabel :: Int
} deriving (Read, Show, Eq, Ord)
instance NFData EmitterState where