Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:1240586
Created September 25, 2011 13:09
toy type inference
module ToyTypeInfer
type Var = string
type Expr =
| ENum of int
| EBool of bool
| EVar of Var
| EPlus of Expr * Expr
| EIf of Expr * Expr * Expr
@einblicker
einblicker / gist:1264362
Created October 5, 2011 12:58
Delimited Continuation
open FSharpx.Continuation
//from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.3425
type DelimitedCont<'A,'B>() =
let metaCont = ref Unchecked.defaultof<'A -> Cont<'A, 'B>>
member private this.Abort (thunk:Cont<'A,'B>):Cont<'A,'B> =
cont {
let! v = thunk
return! !metaCont v
@einblicker
einblicker / gist:1270484
Created October 7, 2011 15:12
reify/reflect
(ql:quickload :cl-cont)
(ql:quickload :alexandria)
(ql:quickload :fare-matcher)
(defpackage :reify-reflect
(:use :cl :cl-cont :fare-matcher :fare-matcher-extensions)
(:import-from :alexandria :iota))
(in-package :reify-reflect)
@einblicker
einblicker / gist:1291761
Created October 17, 2011 01:57
reify/reflect 2
(use 'delimc.core)
(use 'clojure.contrib.monads)
(defmacro reify [thunk]
`(reset
(m-result ~thunk)))
(defmacro reflect [meaning]
`(shift k#
(m-bind ~meaning k#)))
@einblicker
einblicker / gist:1305862
Created October 22, 2011 10:45
lazy list monad
(ql:quickload :pipes)
(ql:quickload :cl-cont)
(ql:quickload :cl-monad-macros)
(defpackage :llmonad
(:use :cl :pipes :cl-cont :cl-monad-macros))
(in-package :llmonad)
(defun pipe-tail-no-cache (pipe)
@einblicker
einblicker / gist:1307616
Created October 23, 2011 17:32
Delimited Continuation in Rhino
function callcc(f) {
return f(new Continuation());
}
importPackage(java.util);
var metaCont = new java.util.Stack();
function abort(thunk) {
return metaCont.peek()(thunk());
@einblicker
einblicker / gist:1308435
Created October 24, 2011 05:37
inner macroexpand
(defun %walker (code test result &optional (cons-fn #'cons))
(labels ((iter (code)
(if (consp code)
(if (and (consp (car code)) (funcall test (car code)))
(funcall cons-fn
(funcall result (car code))
(iter (cdr code)))
(cons
(iter (car code))
(iter (cdr code))))
@einblicker
einblicker / gist:1312238
Created October 25, 2011 10:39
rank-N polymorphism
import scalaz.Forall
case class Record(x : List[Int], y : List[Double])
def lift(
f: Forall[({type X[A] = (List[A], List[A]) => List[A]})#X],
a: Record,
b: Record
): Record =
(a, b) match {
@einblicker
einblicker / gist:1324435
Created October 29, 2011 13:19
hurst exponent
import math.{pow, log, sqrt}
import java.util.Random
import org.apache.commons.math.stat.StatUtils.mean
import org.apache.commons.math.stat.regression.SimpleRegression
object HurstExponent extends App {
def hurstExponent(xs: Array[Double]): Double = {
def S(xs: Array[Double]): Double = {
val meanXs = mean(xs)
@einblicker
einblicker / gist:1325753
Created October 30, 2011 10:05
type-level FizzBuzz
trait Nat {
type IsZero <: Bool
type Prev <: Nat
type Divisable[A <: Nat] = DivisableImpl[A, A]
type DivisableImpl[A <: Nat, B <: Nat] <: Bool
}
trait Z extends Nat {
type IsZero = True
type Prev = Nothing