Skip to content

Instantly share code, notes, and snippets.

@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: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: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
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: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] =
@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: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: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: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: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 =