Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:1338773
Created November 4, 2011 06:14
scalaz and reify/reflect
import util.continuations._
import scalaz._
import Scalaz._
def reify[M[_] : Monad, A](body: => A @cps[M[A]]): M[A] =
reset{val result: A = body; implicitly[Monad[M]].pure[A](result)}
implicit def monad2reflect[M[_] : Monad, A](action: M[A]) = new {
def reflect[B]: A @cps[M[B]] =
shift{(k: A => M[B]) => implicitly[Monad[M]].bind(action, k)}
@einblicker
einblicker / gist:1333909
Created November 2, 2011 15:22
mixed sort
import scalaz.Scalaz._
import scalaz.Order._
import util.control.Exception.allCatch
val RE = """(\d+)|([^\d]+)""".r
def s2i(s: String): Iterable[Either[String, Int]] =
RE.findAllIn(s).map{ s =>
allCatch.either(s.toInt).left.map(_ => s)
}.++(Iterator(Left("dummy"))).toIterable
@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
@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: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: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: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: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: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: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)