This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[_] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |