Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:1485205
Created December 16, 2011 08:55
An observer pattern library by family polymorphism
//library code
//references: http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf
abstract class SubjectObserver {
type S <: Subject
type O <: Observer
abstract class Subject { this: S =>
private var observers: List[O] = List()
def subscribe(obs: O) =
observers = obs :: observers
def publish =
@einblicker
einblicker / gist:1433783
Created December 5, 2011 14:38
sameLength
trait Forall3[X[_, _, _]] {
def apply[U, V, R]: X[U, V, R]
}
type ZipWith[A[_]] = {
type Apply[U, V, R] = ((U, V) => R) => (A[U], A[V]) => A[R]
}
type Result[T] = (A[T], A[T], Forall3[ZipWith[A]#Apply]) forSome { type A[_] }
@einblicker
einblicker / gist:1404219
Created November 29, 2011 09:51
perceptron
object Perceptron {
type Vec = Map[Symbol, Double]
def makeVec(R: Double, G: Double, B: Double, bias: Double): Vec =
Map('R -> R, 'G -> G, 'B -> B, 'bias -> bias)
def predict(w: Vec, x: Vec): Double =
w.keys.filter(w(_) != 0).map(k => w(k) * x(k)).sum
def train(w: Vec, x: Vec, t: Double): Vec =
@einblicker
einblicker / gist:1402327
Created November 28, 2011 22:12
huffman encoding
object Huffman extends App {
sealed trait Tree { val weight: BigDecimal }
case class Leaf(override val weight: BigDecimal) extends Tree
case class Node(override val weight: BigDecimal, lhs: Tree, rhs: Tree) extends Tree
def mkHuffmanTree(input: List[BigDecimal]) = {
var xs: List[Tree] = input.sorted.map(x => Leaf(x))
Iterator.iterate(xs){
xs0 => {
val xs1 = xs0.sortWith(_.weight < _.weight)
@einblicker
einblicker / gist:1402318
Created November 28, 2011 22:11
encoding data families by using dependent method types
trait GMap[+V] {
type P[+_]
val content: P[V]
}
trait GMapKey[K] {
type GM[+V] <: GMap[V]
def empty[V]: GM[V]
def lookup[V](k: K, m: GM[V]): Option[V]
def insert[V](k: K, v: V, m: GM[V]): GM[V]
}
@einblicker
einblicker / gist:1385350
Created November 22, 2011 10:10
FastForall
import scalaz.Forall
object FastForall {
type Not[A] = A => Unit
type DNE[P[_]] = Not[P[A]] forSome { type A }
type CPS[P[_]] = Not[DNE[P]]
def apply[P[_]](p: CPS[P]): Forall[P] = new Forall[P] {
private var cache: Option[P[_]] = None
def apply[A]: P[A] = synchronized {
@einblicker
einblicker / gist:1359705
Created November 11, 2011 23:51
dynamic scope emulation by reader monad
import scalaz._
import Scalaz._
import util.continuations._
import collection.mutable.LinkedHashMap
object DynamicScopeEmulation extends App {
//see also: https://gist.github.com/1338773
object ReifyReflect {
def reify[M[_] : Monad, A](body: => A @cps[M[A]]): M[A] =
@einblicker
einblicker / gist:1356436
Created November 10, 2011 22:19
monadic memoization
import scalaz._
import Scalaz._
object MonadicMemoization extends App {
type Memo[Arg, Ret] = State[Map[Arg, Ret], Ret]
def ret[Arg, Ret](r: Ret): Memo[Arg, Ret] =
state[Map[Arg, Ret], Ret](x => (x, r))
def memo[Arg, Ret](s: Arg => Memo[Arg, Ret])(k: Arg): Memo[Arg, Ret] =
import util.continuations._
class Holder[A] {
var value: A = _
def foo(x: => A @suspendable): Unit =
reset { value = x }
}
object Test extends App {
val holder = new Holder[Double]
@einblicker
einblicker / gist:1341454
Created November 5, 2011 12:30
reactive programming
import util.continuations._
class DataflowVar[A] {
private var value_ : A = _
private var conts: List[A => Unit] = Nil
def apply(): A @suspendable =
shift { (k: A => Unit) =>
conts ::= k